7

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

Fabio Cenni
  • 841
  • 3
  • 16
  • 30
  • I think you need to set "length" to 2 – Tikhonov Aleksandr Aug 19 '15 at 11:17
  • @TikhonovAlexander if i set length to 2 the app crashes with an index out of range -.- I don't know how is it possible to set... – Fabio Cenni Aug 19 '15 at 11:27
  • Actually, after second call `stringNumeroMutable.addAttribute`, you need to set `cell.numeroCori.attributedText = stringNumeroMutable` again. And insted of `NSRange(location: 0, length: 1)` you should use `NSRange(location: 0, length: stringNumeroMutable.length)` – Tikhonov Aleksandr Aug 19 '15 at 11:58
  • @TikhonovAlexander I tried this but does not work again (I edited the question) – Fabio Cenni Aug 22 '15 at 11:59

1 Answers1

7

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.

Tikhonov Aleksandr
  • 13,945
  • 6
  • 39
  • 53
  • Sorry for my late answer! I saw your solution and it works, but is there no way to make bold a string or mutableString? – Fabio Cenni Sep 04 '15 at 22:55