65

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.

Sujith Thankachan
  • 3,508
  • 2
  • 20
  • 25
jaydev
  • 1,629
  • 5
  • 17
  • 31
  • 4
    Possible duplicate of [How can I concatenate NSAttributedStrings?](http://stackoverflow.com/questions/18518222/how-can-i-concatenate-nsattributedstrings) – jtbandes Apr 07 '16 at 06:16

5 Answers5

158

Use NSMutableAttributedString to achieve that.

Example

Swift 5

let yourAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 15)]
let yourOtherAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red, NSAttributedString.Key.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) 

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) 
rptwsthi
  • 10,094
  • 10
  • 68
  • 109
glace
  • 2,162
  • 1
  • 17
  • 23
  • 11
    I was surprised to find that, still in Swift 3, it seems to be essential to initialize the NSMutableAttributedString empty in order to append to it. – Matt Bearson Dec 08 '16 at 13:43
  • 1
    I smell the sarcasm, but it is seriously strange that one cant just append partTwo to partOne without an empty initialized NSMAString.... – glace Jan 12 '17 at 12:34
20

@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)
Lou Zell
  • 5,255
  • 3
  • 28
  • 23
leanne
  • 7,940
  • 48
  • 77
  • By the time of my answer, the empty declaration of the combiner was necessary. Im off from Swift since then, cant confirm if changes in Swift 3.1 are making this possible. – glace Aug 21 '17 at 08:15
9

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
Community
  • 1
  • 1
Vivek
  • 4,916
  • 35
  • 40
2

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)
cnu
  • 443
  • 4
  • 9
0

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()
Celiuska
  • 45
  • 6