Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use multiple font colors in a single label

Is there a way to use two, or even three font colors in a single label in iOS?

If the text "hello, how are you" were used as an example, the "hello," would be blue, and the "how are you" would be green?

Is this possible, it seems easier than creating multiple labels?

like image 719
Justin Rose Avatar asked Jan 01 '15 05:01

Justin Rose


People also ask

How do you change the color of a label in Xcode?

There should be a text color box in interface builder, but you can also do it through code. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,100)] label. textColor = [UIColor redColor]; Or just use the reference that you already have to the text label.

How do I color a string in Swift?

The main here is to use a NSMutableAttributedString and the selector addAttribute:value:range with the attribute NSForegroundColorAttributeName to change a color of a string range: NSMutableAttributedString *attrsString = [[NSMutableAttributedString alloc] initWithAttributedString:label.

How do you bold a string in Swift?

let label = UILabel() label. attributedText = NSMutableAttributedString() . bold("Address: ") . normal(" Kathmandu, Nepal\n\n") .


2 Answers

Reference from here.

First of all initialize of you NSString and NSMutableAttributedString as below.

var myString:NSString = "I AM KIRIT MODI" var myMutableString = NSMutableAttributedString() 

In ViewDidLoad

override func viewDidLoad() {      myMutableString = NSMutableAttributedString(string: myString, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])     myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location:2,length:4))     // set label Attribute     labName.attributedText = myMutableString     super.viewDidLoad() } 

OUTPUT

enter image description here

MULTIPLE COLOR

Add the line code below in your ViewDidLoad to get multiple colors in a string.

 myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.greenColor(), range: NSRange(location:10,length:5)) 

Multiple color OUTPUT

enter image description here

Swift 4

var myMutableString = NSMutableAttributedString(string: str, attributes: [NSAttributedStringKey.font :UIFont(name: "Georgia", size: 18.0)!]) myMutableString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red, range: NSRange(location:2,length:4)) 

Swift 5.0

 var myMutableString = NSMutableAttributedString(string: str, attributes: [NSAttributedString.Key.font :UIFont(name: "Georgia", size: 18.0)!])  myMutableString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red, range: NSRange(location:2,length:4)) 
like image 66
Kirit Modi Avatar answered Oct 13 '22 07:10

Kirit Modi


For @Hems Moradiya

enter image description here

let attrs1 = [NSFontAttributeName : UIFont.boldSystemFontOfSize(18), NSForegroundColorAttributeName : UIColor.greenColor()]  let attrs2 = [NSFontAttributeName : UIFont.boldSystemFontOfSize(18), NSForegroundColorAttributeName : UIColor.whiteColor()]  let attributedString1 = NSMutableAttributedString(string:"Drive", attributes:attrs1)  let attributedString2 = NSMutableAttributedString(string:"safe", attributes:attrs2)  attributedString1.appendAttributedString(attributedString2) self.lblText.attributedText = attributedString1 

Swift 4

    let attrs1 = [NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 18), NSAttributedStringKey.foregroundColor : UIColor.green]      let attrs2 = [NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 18), NSAttributedStringKey.foregroundColor : UIColor.white]      let attributedString1 = NSMutableAttributedString(string:"Drive", attributes:attrs1)      let attributedString2 = NSMutableAttributedString(string:"safe", attributes:attrs2)      attributedString1.append(attributedString2)     self.lblText.attributedText = attributedString1 

Swift 5

    let attrs1 = [NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 18), NSAttributedString.Key.foregroundColor : UIColor.green]      let attrs2 = [NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 18), NSAttributedString.Key.foregroundColor : UIColor.white]      let attributedString1 = NSMutableAttributedString(string:"Drive", attributes:attrs1)      let attributedString2 = NSMutableAttributedString(string:"safe", attributes:attrs2)      attributedString1.append(attributedString2)     self.lblText.attributedText = attributedString1 
like image 39
Keyur Hirani Avatar answered Oct 13 '22 08:10

Keyur Hirani