6

How can i have a UILabel with two different colors for the font? I will have text in two different strings and i want to make text with first string as red and second as green. The length of both the string is variable.

1

5 Answers 5

8

Try TTTAttributedLabel. It's a subclass of UILabel that supports NSAttributedStrings, which would make it easy to have multiple colors, fonts, and styles in the same string.


Edit: Alternatively, if you don't want the 3rd party dependency and are targeting iOS 6, UILabel now has the attributedText property.

1
  • 1
    Nice one. NSAttributedString is definitely the way to go - and the class you're suggesting seems very nice.
    – iceydee
    May 9, 2011 at 19:27
7

You can't do this within a UILabels. But my suggestion is that instead of using multiple UILabel just concentrate on NSAttributedString. Find UIControllers that draw NSAttributedString because UILabel, UITextView do not support NSAttributedString.

PS: if you plan to distribute an iOS6 or later application, as UILabel now support NSAttributedString, you should use UILabel directly instead of OHAttributedLabel as it is now natively supported by the OS.

2
  • This answer is not correct any more because e.g. UILabel does support NSAttributedString since iOS 6.
    – brainray
    Jun 10, 2014 at 19:49
  • @brainray I have updated the answer late year itself. Please check it. Jun 11, 2014 at 18:08
4

UILabel can only have one color. You either need a more sophisticated element, or - probably easier - just use two separate labels. Use [yourLabel sizeToFit]; and place them accordingly.

1
  • okkk i will try to explore sizeToFit property of uilabel, thanks for replying
    – pankaj
    Oct 17, 2010 at 8:48
1

Swift 4
(Note: notation for attributed string key is changed in swift 4)

Here is an extension for NSMutableAttributedString, that add/set color on string/text.

extension NSMutableAttributedString {

    func setColor(color: UIColor, forText stringValue: String) {
        let range: NSRange = self.mutableString.range(of: stringValue, options: .caseInsensitive)
        self.addAttribute(NSAttributedStringKey.foregroundColor, value: color, range: range)
    }

}

Now, try above extension with UILabel and see result

let label = UILabel()
label.frame = CGRect(x: 40, y: 100, width: 280, height: 200)
let red = "red"
let blue = "blue"
let green = "green"
let stringValue = "\(red)\n\(blue)\n&\n\(green)"
label.textColor = UIColor.lightGray
label.numberOfLines = 0
let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: stringValue)
attributedString.setColor(color: UIColor.red, forText: red)   // or use direct value for text "red"
attributedString.setColor(color: UIColor.blue, forText: blue)   // or use direct value for text "blue"
attributedString.setColor(color: UIColor.green, forText: green)   // or use direct value for text "green"
label.font = UIFont.systemFont(ofSize: 26)
label.attributedText = attributedString
self.view.addSubview(label)


Here is solution in 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(NSForegroundColorAttributeName, value: color, range: range)
          }
        }

}


func multicolorTextLabel() {
        var string: NSMutableAttributedString = NSMutableAttributedString(string: "red\nblue\n&\ngreen")
        string.setColorForText(textToFind: "red", withColor: UIColor.red)
        string.setColorForText(textToFind: "blue", withColor: UIColor.blue)
        string.setColorForText(textToFind: "green", withColor: UIColor.green)
        labelObject.attributedText = string
    }

Result:

enter image description here

4
  • Just tried this in iOS 11 and got an error in the extension.. self.addAttributes([NSForegroundColorAttributeName : color], range: range)
    – August
    Feb 19, 2018 at 20:52
  • @August - Please share error message. Also let me know swift version of your project. You may be trying with Swift 3 extension and your project has Swift 4. Please share error related info, so I can help you.
    – Krunal
    Feb 20, 2018 at 5:07
  • Yes, it is a Swift 4 project and used the Swift 4 extension you shared. It was a syntax error, not a compile/runtime error. Doc » developer.apple.com/documentation/foundation/…
    – August
    Feb 21, 2018 at 1:52
  • 1
    ...and sorry for the vague comment, I posted it and forgot to make sure it actually worked. As a result, it took a bit longer than the 5 minute edit time limit. Thanks for following up!
    – August
    Feb 21, 2018 at 1:55
0

In iOS 6 UILabel has NSAttributedString property. So use that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.