Add several attributes to a NSMutableAttributedString

Fabio Cenni picture Fabio Cenni · Aug 19, 2015 · Viewed 9.2k times · Source

I'm trying to add several attributes to a NSMutableAttributedString; i tried this:

let stringNumero: NSString = "\(squadra.cori.count)" //= two-digit number
var stringNumeroMutable = NSMutableAttributedString()
stringNumeroMutable = NSMutableAttributedString(string: stringNumero as! String, attributes: [NSFontAttributeName: UIFont(name: "Noteworthy-Light", size: 9)!,
                                                                                                  NSForegroundColorAttributeName: UIColor(red: 110/255.0, green: 183/255.0, blue: 93/255.0, alpha: 1.0)])
cell.numeroCori.attributedText = stringNumeroMutable

now I'd like to add an other attributes and i'm going uso this code:

stringNumeroMutable.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFontOfSize(8), range: NSRange(location: 0, length: 1))

but I'm incurring in a problem:

enter image description here

as you see, the bold effect is attributed only to the first digit and the previous attributed (such as the font) disappear. I think the the second NSFontAttributedName subscribe the previous one, so is there any way to set both font and bold? Thanks a lot!!!

Edit: as suggested i tried to recall the method like this:

let stringNumero: NSString = "\(squadra.cori.count)" //= two-digit number
var stringNumeroMutable = NSMutableAttributedString()
stringNumeroMutable = NSMutableAttributedString(string: stringNumero as! String, attributes: [NSFontAttributeName: UIFont(name: "Noteworthy-Light", size: 9)!,
                                                                                                  NSForegroundColorAttributeName: UIColor(red: 110/255.0, green: 183/255.0, blue: 93/255.0, alpha: 1.0)])
cell.numeroCori.attributedText = stringNumeroMutable

stringNumeroMutable.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFontOfSize(9), range: NSRange(location: 0, length: stringNumeroMutable.length))

cell.numeroCori.atributedText = stringNumeroMutable

but the second call overwrite the first one (so the number loose the attributed font). How to solve this?

enter image description here

Answer

Tikhonov Alexander picture Tikhonov Alexander · Aug 22, 2015

I think your problem, that you use method boldSystemFontOfSize

boldSystemFontOfSize:

Returns the font object used for standard interface items that are rendered in boldface type in the specified size.

See my solution:

@IBOutlet weak var label: UILabel!
var mutableAttributedString: NSMutableAttributedString!
override func viewDidLoad() {
    super.viewDidLoad()
    let someString = "Hello World"
    let attr: [String: AnyObject] = [NSFontAttributeName: UIFont(name: "Helvetica", size: 10)!,
        NSForegroundColorAttributeName: UIColor(red: 0.18, green: 0.18, blue: 0.18, alpha: 1.0)]
    mutableAttributedString = NSMutableAttributedString(string: someString, attributes: attr)
    label.attributedText = mutableAttributedString
}

@IBAction func buttonPressed(sender: UIButton) {
    let font = mutableAttributedString.attribute(NSFontAttributeName, atIndex: 1, effectiveRange: nil)!
    mutableAttributedString.addAttribute(NSFontAttributeName, value: font.fontWithSize(20) , range: NSMakeRange(0, mutableAttributedString.length))
    label.attributedText = mutableAttributedString
}

In buttonPressed function you define current font of mutableAttributedString and then you can change font's size with method fontWithSize.