How to set multiple font colors in a single label in Swift?

According to normal string in iOS, There is not provide the facility of colorful string in iOS, but have a class NSAttributeString which have provide color of string facility. For formatting a part of a string, you need an attributed string which specifies parts of the string formatted in different ways. In this lesson we’ll learn how to work in attributed strings in Swift.




 First of all you Setup new project, and take different label in the View Controller. Here I am taking six label and all label connects IBOutlet in ViewController.swift file.

Initializing an Attributed String

First of all initializing Attributed string, so adding below code in ViewDidLoad method.

var myString:NSString = "I AM KIRIT MODI"
var myMutableString = NSMutableAttributedString()
myMutableString = NSMutableAttributedString(string: myString as 
String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])
As above Code, The first string myString which want to colorful string, and second is NSMutableAttributedString add attribute.

Lable-1: Change the color of single string.

In the label an attributed string with the string myString and an attribute dictionary. Here we used an attribute named NSForegroundColorAttributeName as the key, and a UIColor.redColor() object as the value. and also passing range of which want to change NSRange(location:2,length:2) Build and run. Code as below of first label.
myMutableString = NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])
        myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location:2,length:2))
 lbl_First.attributedText = myMutableString

Lable-2: Change the color of more string.

Same follow as above steps but adding here three attribute NSForegroundColorAttributeName with three different color name and different range value, Code as below of second label.
myMutableString = NSMutableAttributedString()
myMutableString = NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location:2,length:2))
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.greenColor(), range: NSRange(location:5,length:5))
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueColor(), range: NSRange(location:11,length:4))
lbl_Second.attributedText = myMutableString

Lable-3: Change the color and font of string.

we used an attribute named NSFontAttributeName as the key, and a UIFont(name: "AmericanTypewriter-Bold", size: 18.0)! object as the value. and also passing range of which want to changeNSRange(location:5,length:5)  Build and run. Code as below of first label.
myMutableString = NSMutableAttributedString()
myMutableString = NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location:2,length:2))
myMutableString.addAttribute(NSFontAttributeName, value: UIFont(name: "AmericanTypewriter-Bold", size: 18.0)!, range: NSRange(location:5,length:5))
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueColor(), range: NSRange(location:11,length:4))
lbl_Third.attributedText = myMutableString

Lable-4: Change the stroke color and width of string.

we used two attribute named NSStrokeColorAttributeName as the key, and a UIColor.blueColor() object as the value. NSStrokeWidthAttributeName as the key, and a "2" object as the value. and give the same range both adding attribute.  Build and run. Code as below forth label.

myMutableString = NSMutableAttributedString()
myMutableString = NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])
myMutableString.addAttribute(NSFontAttributeName,
            value: UIFont(name: "Georgia",size: 36.0)!,range: NSRange(location: 0,length: 1))
myMutableString.addAttribute(NSStrokeColorAttributeName,value: UIColor.blueColor(),range:  NSRange(location: 0,length: 1))
myMutableString.addAttribute(NSStrokeWidthAttributeName,value: 2,range: NSRange(location: 0,length: 1))
lbl_Forth.attributedText = myMutableString

Lable-5: Change the background color of string

we used attribute named NSBackgroundColorAttributeName as the key, and a  UIColor.yellowColor() object as the value. and range with full string length, Build and run. Code as below fifth label.

myMutableString = NSMutableAttributedString()
myMutableString = NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])
myMutableString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.yellowColor(), range: NSRange(location:0,length:myString.length))
lbl_Fifth.attributedText = myMutableString

Lable-6: Change the background color of  individual string

we used attribute named NSBackgroundColorAttributeName property for individual range, and change background color according to range, Code as below sixth label.
myMutableString = NSMutableAttributedString()
myMutableString = NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location:2,length:2))
myMutableString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.yellowColor(), range: NSRange(location:2,length:2))
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.greenColor(), range: NSRange(location:5,length:5))
myMutableString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.blueColor(), range: NSRange(location:5,length:5))
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueColor(), range: NSRange(location:11,length:4))
myMutableString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.orangeColor(), range: NSRange(location:11,length:4))lbl_Six.attributedText = myMutableString
Demo Project:
This demo project contain about multiple string colored, stroke, background colors , fonts
.
Thanks, I will come with new topic.

How to set multiple font colors in a single label in Swift? How to set multiple font colors in a single label in Swift? Reviewed by Kirit Modi on 23:14:00 Rating: 5

No comments:

Powered by Blogger.