iosswiftcolorswatchkitwkinterfacelabel

How can I color a part of a WKInterfaceLabel


I have a sentence and I want to color some parts of this sentence.

For example "Turn on the lights" will become "Turn on(in blue) the lights(in red)"

Do you know if it's possible to make it with a unique label ?

If it's not possible, do you know how can I manage some labels side by side to print them correctly ? (with return line when text is too long, without white space...)


Solution

  • Just use an attributed string.

    let s = NSMutableAttributedString(string: "Turn on", attributes: [NSForegroundColorAttributeName: UIColor.blueColor()])
    s.appendAttributedString(NSAttributedString(string: " the lights", attributes: [NSForegroundColorAttributeName : UIColor.redColor()]))
    
    label.setAttributedText(s)