UILabel with two different color text
Asked Answered
A

5

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.

Aoudad answered 17/10, 2010 at 8:40 Comment(1)
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 timeDefamation
C
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.

Coffle answered 15/11, 2010 at 11:4 Comment(2)
This answer is not correct any more because e.g. UILabel does support NSAttributedString since iOS 6.Alee
@Alee I have updated the answer late year itself. Please check it.Coffle
H
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.

Homeland answered 9/5, 2011 at 19:21 Comment(1)
Nice one. NSAttributedString is definitely the way to go - and the class you're suggesting seems very nice.Trust
C
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.

Coffle answered 15/11, 2010 at 11:4 Comment(2)
This answer is not correct any more because e.g. UILabel does support NSAttributedString since iOS 6.Alee
@Alee I have updated the answer late year itself. Please check it.Coffle
M
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.

Macrobiotics answered 17/10, 2010 at 8:44 Comment(1)
okkk i will try to explore sizeToFit property of uilabel, thanks for replyingAoudad
S
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

Sublimate answered 6/7, 2017 at 16:51 Comment(4)
Just tried this in iOS 11 and got an error in the extension.. self.addAttributes([NSForegroundColorAttributeName : color], range: range)Wadmal
@Wadmal - 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.Sublimate
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/…Wadmal
...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!Wadmal
B
0

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

Bilabial answered 26/6, 2013 at 9:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.