Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add several attributes to a NSMutableAttributedString

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

like image 426
Fabio Cenni Avatar asked Aug 19 '15 10:08

Fabio Cenni


People also ask

What is nsmutableattributedstring?

A mutable string with associated attributes (such as visual style, hyperlinks, or accessibility data) for portions of its text. The NSMutableAttributedString class declares additional methods for mutating the content of an attributed string. You can add and remove characters (raw strings) and attributes separately or together as attributed strings.

What is NSAttributedString in iOS 10?

Extension for rich Push Notification - iOS 10. NSAttributedString (and its mutable sibling NSMutableAttributedString) allows you to create strings that are complex in their appearance to the user. A common application is to use this to display a string and adding custom kerning / letter-spacing.

How do I mutate the content of an attributed string?

The NSMutableAttributedString class declares additional methods for mutating the content of an attributed string. You can add and remove characters (raw strings) and attributes separately or together as attributed strings. See the class description for NSAttributedString for more information about attributed strings.

What is the default font for NSAttributedString objects?

Note that the default font for NSAttributedString objects is Helvetica 12-point, which may differ from the macOS system font, so you may wish to create the string with non-default attributes suitable for your application using, for example, init(string:attributes:).


1 Answers

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.

like image 174
Tikhonov Aleksandr Avatar answered Sep 20 '22 12:09

Tikhonov Aleksandr