how to append Attributed Text String with Attributed String in Swift

nsmutableattributedstring
concatenate attributed strings swift
nsattributedstring alignment swift 4
change font size of attributed string swift
swift attributed string range
nsattributedstring clickable text swift
bold non bold text in a single uilabel
nsmutableattributedstring objective-c

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.

Use NSMutableAttributedString

Small example

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) 

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

NSAttributedString by example – Hacking with Swift, append(_:). Adds the characters and attributes of a given attributed string to the end of the receiver. Declaration. func append(_ attrString: NSAttributedString)  We will need a string and attributed text string. I’m using a NSMutableAttributedString so I can add attributes as we go along in the lesson. If you have only one time you set attributes, you can do that with a NSAttributedString. Initializing an Attributed String. in the viewDidLoad method, add the following code:

@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)

append(_:), Attributed strings allow you to format ranges of text with custom colors, about creating attributed strings still don't feel very Swiftlike (like NSRange, them by making an empty Dictionary and then adding key-value pairs to it. let myAddedString = AttributedString( string: "!!!", attributes: myAddedStringAttributes) myMutableString.append(myAddedString) Build and run. This is just the start of what you can do with attributed text. The Whole Code. This code is compatible with both Projects and Playgrounds.

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

Attributed Strings in Swift - Suragch, In any Swift file where you want to use BonMot, simply import BonMot . Basics. Use a StringStyle to specify the style of your attributed string. Then Composable : a protocol defining any type that knows how to append itself to an attributed string. space between image and text "label with icon", // raw or attributed string ]). edit/update: Xcode 8.3.2 • Swift 3.1 If you know HTML and CSS you can use it to easily control the font style, color and size of your attributed string as follow:

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)

Rightpoint/BonMot: Beautiful, easy attributed strings in Swift, Although you can apply attributes to a range of strings, sometimes it's just easier to append two attributed strings rather than mess with ranges. //create attributed string from string with attributes let attributedString = NSAttributedString(string: "ios-tutorial.com", attributes: [NSFontAttributeName : UIFont( name: "Chalkduster", size: 22.0)!]) Apply different text color to character range

How do I append attributed strings in Swift?, Attributed text lets you style strings without changing the style on the label directly​. Here's a guide for or string. In this case, it's useful to know about attributedText on UILabels. NSString *helloWorldString = @"Hello World! Get NSString from NSMutableArray on Swift. swift,nsmutablearray. Try componentsJoinedByString func cualidadesToString() -> NSString{ return cualidades.componentsJoinedByString(",") } But you should use Swift's builtin String and Array types to make it more type-safe. var cualidades = [String]() func addCualidad(cualidad: String){ cualidades.append(cualidad) } func delCualidad(cualidad: String

Create a Single UILabel with Multiple Styles using Attributed Text, Appending Attributed Strings and bold text in Swift. let someValue : String = "​Something the user entered" let text = NSMutableAttributedString(string: "The value  You can add attributes after concatenating the attributed strings. Just the the range of the string and add the attribute with addAttribute(_:value:range:). let attrTitle = NSAttributedString(string: "Title") let attrBody = NSAttributedString(string: "Body") let mutableAttributedString = NSMutableAttributedString() mutableAttributedString.append(attrTitle) mutableAttributedString.append

Appending Attributed Strings and bold text in Swift, Finally, we're setting the text on a UILabel to the new attributed string. attributes​: worldAttributes) swiftString.append(hereString) myLabel. Attributed String in Swift: the right way to set attributes for text; attributes identifiers (as strings) are passed along with their values. a base common style for your string and add

Comments
  • Possible duplicate of How can I concatenate NSAttributedStrings?
  • 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.
  • I smell the sarcasm, but it is seriously strange that one cant just append partTwo to partOne without an empty initialized NSMAString....
  • 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.