开发者

UILabel Text with Multiple Font Colors

Is there a way to have the textColor property of a UILabel be two different UIColors开发者_StackOverflow中文版? Basically I'm trying to make the first character in the UILabel text property to be greenColor and the rest be blackColor. I would like to avoid using two different UILabels because I may want to change the position in the text of the green character.


Starting from ios 6 you can do the following :

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]  initWithString:string];
[attributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,3)];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(4,9)];
cell.label.attributedText = attributedString;


UILabel doesnot supprt this property...

Use should use NSAttributedString... and use controllers for drawing NSAttributesString...

Controller for NSAttributedString

UPDATE:

From iOS 6 you can do the following :

label.attributedText = attributedString;


Swift 3:

extension NSMutableAttributedString {
        func setColorForText(textToFind: String, withColor color: UIColor) {
         let range: NSRange = self.mutableString.range(of: textToFind, options: .caseInsensitive)
          if range != nil {
            self.addAttribute(NSAttributedStringKey.foregroundColor, value: color, range: range)
          }
        }

}


func setColoredLabel() {
        var string: NSMutableAttributedString = NSMutableAttributedString(string: "My label with red blue and green colored text")
        string.setColorForText(textToFind: "red", withColor: UIColor.red)
        string.setColorForText(textToFind: "blue", withColor: UIColor.blue)
        string.setColorForText(textToFind: "green", withColor: UIColor.green)
        mylabel.attributedText = string
    }

Result:

UILabel Text with Multiple Font Colors


NSAttributedStrings support mixed fonts, styles, and colors in the same string, but aren't currently supported by any standard UIKit controls. That said, you should check out TTTAttributedLabel. It's a simple, performant replacement for UILabel that will allow you to display rich text really easily.


No - Apple say you should use HTML in a UIWebView if you need formatted text. (See the note in the overview section of the UITextView API docs.)


Another alternative way is to create multiple labels with difference color and layout them next to each other. Try to make the label's background color transparent. It may be tedious but should work.


No, that's not possible - you need to draw on your own or compose using several labels.

If you can accept 3.2 compatibility, you might have a look at NSAttributedString.


not currently possible, usually you would use an NSAttributedString, but to use this on iOS you would need to roll your own label. you may be able to work round this using a UIWebView, but I don't like to do that, it seems heavy handed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜