how to append Attributed Text String with Attributed String in Swift

SwiftNsattributedstring

Swift Problem Overview


I want to append an Attributed Text with another Attributed Text in Swift. Please provide any sample code for appending operation of two attributed String in Swift.

Swift Solutions


Solution 1 - Swift

Use NSMutableAttributedString to achieve that.

Example

Swift 5
let yourAttributes = [NSForegroundColorAttributeName: UIColor.black, NSFontAttributeName: UIFont.systemFont(ofSize: 15)]
let yourOtherAttributes = [NSForegroundColorAttributeName: UIColor.red, NSFontAttributeName: UIFont.systemFont(ofSize: 25)]

let partOne = NSMutableAttributedString(string: "This is an example ", attributes: yourAttributes)
let partTwo = NSMutableAttributedString(string: "for the combination of Attributed String!", attributes: yourOtherAttributes)

partOne.append(partTwo) 
Swift 3
let yourAttributes = [NSForegroundColorAttributeName: UIColor.black, NSFontAttributeName: UIFont.systemFont(ofSize: 15)]
let yourOtherAttributes = [NSForegroundColorAttributeName: UIColor.red, NSFontAttributeName: UIFont.systemFont(ofSize: 25)]

let partOne = NSMutableAttributedString(string: "This is an example ", attributes: yourAttributes)
let partTwo = NSMutableAttributedString(string: "for the combination of Attributed String!", attributes: yourOtherAttributes)

let combination = NSMutableAttributedString()

combination.append(partOne)
combination.append(partTwo)

combination represents your final string which contains both formattings provided by yourAttributes and yourOtherAttributes

Even older
let yourAttributes = [NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: UIFont.systemFontOfSize(15)]
let yourOtherAttributes = [NSForegroundColorAttributeName: UIColor.redColor(), NSFontAttributeName: UIFont.systemFontOfSize(25)]

let partOne = NSMutableAttributedString(string: "This is an example ", attributes: yourAttributes)
let partTwo = NSMutableAttributedString(string: "for the combination of Attributed String!", attributes: yourOtherAttributes)

let combination = NSMutableAttributedString()

combination.appendAttributedString(partOne)
combination.appendAttributedString(partTwo) 

Solution 2 - Swift

@glace's answer, modified to avoid empty NSMutableAttributedString declaration. Valid in Swift 3.1:

let yourAttributes = [NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: UIFont.systemFontOfSize(15)]
let yourOtherAttributes = [NSForegroundColorAttributeName: UIColor.redColor(), NSFontAttributeName: UIFont.systemFontOfSize(25)]

let partOne = NSMutableAttributedString(string: "This is an example ", attributes: yourAttributes)
let partTwo = NSMutableAttributedString(string: "for the combination of Attributed String!", attributes: yourOtherAttributes)

partOne.append(partTwo)

partOne is then your final string with all the attributes. No intermediate "combiner" necessary.

Swift 4

let yourAttributes: [NSAttributedString.Key: Any] = [.foregroundColor: UIColor.black, .font: UIFont.systemFont(ofSize: 15)]
let yourOtherAttributes: [NSAttributedString.Key: Any] = [.foregroundColor: UIColor.red, .font: UIFont.systemFont(ofSize: 25)]

let partOne = NSMutableAttributedString(string: "This is an example ", attributes: yourAttributes)
let partTwo = NSMutableAttributedString(string: "for the combination of Attributed String!", attributes: yourOtherAttributes)

partOne.append(partTwo)

Solution 3 - Swift

Swift 5

As per "glace" answer, I just update font attribute and swift version.

    let boldFontAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black, NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 17)]
    let normalFontAttributes = [NSAttributedString.Key.foregroundColor: UIColor.darkGray, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 15)]
    let partOne = NSMutableAttributedString(string: "This is an example ", attributes: boldFontAttributes)
    let partTwo = NSMutableAttributedString(string: "for the combination of Attributed String!", attributes: normalFontAttributes)

    let combination = NSMutableAttributedString()
    
    combination.append(partOne)
    combination.append(partTwo)
    lblUserName.attributedText = combination

Solution 4 - Swift

using extension,

extension NSMutableAttributedString{
    func getAttributedStringByAppending(attributedString:NSMutableAttributedString) -> NSMutableAttributedString{
        let newAttributedString = NSMutableAttributedString()
        newAttributedString.append(self)
        newAttributedString.append(attributedString)
        return newAttributedString
    }
}

Usage: attributedString1, attributedString2 are two NSMutableAttributedString, then

let combinedAttributedString = attributedString1.getAttributedStringByAppending(attributedString: attributedString2)

Solution 5 - Swift

Swift 5

You can create the different attributes:

let kString1Attributes = NSAttributedString.attributes(foregroundColor: UIColor.white)
let kString2Attributes = NSAttributedString.attributes(foregroundColor: UIColor.black)

Define your attributed strings:

let attrString1 = NSAttributedString(string: "my string" + " ",
                                     attributes: kString1Attributes)
let attrString2 = NSAttributedString(string: "string 2",
                                     attributes: kString2Attributes)

Include them in an array and combine them all together:

let combinedAttrString = [attrString1, attrString2].joined()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjaydevView Question on Stackoverflow
Solution 1 - SwiftglaceView Answer on Stackoverflow
Solution 2 - SwiftleanneView Answer on Stackoverflow
Solution 3 - SwiftVivekView Answer on Stackoverflow
Solution 4 - SwiftcnuView Answer on Stackoverflow
Solution 5 - SwiftCeliuskaView Answer on Stackoverflow