Author Topic: How do I append attributed strings in Swift?  (Read 10777 times)

kjmcneish

  • Administrator
  • *****
  • Posts: 719
    • View Profile
How do I append attributed strings in Swift?
« on: April 07, 2015, 03:37:16 PM »
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.

You can append attributed strings only if the first string is a mutable attributed string. The following code appends an underlined attributed string to a regular attributed string:

Code: [Select]
var underlineAttribute = [NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue]
var mutableAttString = NSMutableAttributedString(string: Settings.sharedInstance.player1Name, attributes: underlineAttribute)
mutableAttString.appendAttributedString(NSAttributedString(string: ": \(Settings.sharedInstance.playerNumberOfWins)"))

All the best!
Kevin McNeish
Author of Learn to Code in Swift: https://itunes.apple.com/us/book/learn-to-code-in-swift/id942956811?mt=11
« Last Edit: April 07, 2015, 04:16:39 PM by kjmcneish »