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.

Krunal
  • 77,632
  • 48
  • 245
  • 261
pankaj
  • 7,878
  • 16
  • 69
  • 115
  • I had to do that too in a project. I used this class from another post but it's only for one **single** line, I don't know if it is enough for you. [iPhone - UILabel containing text with multiple fonts at the same time](http://stackoverflow.com/questions/1417346/iphone-uilabel-containing-text-with-multiple-fonts-at-the-same-time) – William Remacle Oct 17 '10 at 10:25

5 Answers5

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.

mattt
  • 19,544
  • 7
  • 73
  • 84
  • 1
    Nice one. NSAttributedString is definitely the way to go - and the class you're suggesting seems very nice. – iceydee May 09 '11 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.

KingofBliss
  • 15,055
  • 6
  • 50
  • 72
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.

Eiko
  • 25,601
  • 15
  • 56
  • 71
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

Krunal
  • 77,632
  • 48
  • 245
  • 261
  • Just tried this in iOS 11 and got an error in the extension.. self.addAttributes([NSForegroundColorAttributeName : color], range: range) – August Feb 19 '18 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 '18 at 05: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 » https://developer.apple.com/documentation/foundation/nsmutableattributedstring/1414304-addattributes – August Feb 21 '18 at 01: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 '18 at 01:55
0

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

Mann
  • 5,477
  • 6
  • 45
  • 57