How To Set Multiple Font Colors in a Single label in Swift 3.0?

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.

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)!])


Label-1: Change the colour 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.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSRange(location:2,length:2))
lbl_First.attributedText = myMutableString

Label-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.red, range: NSRange(location:2,length:2))
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.green, range: NSRange(location:5,length:5))
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blue, range: NSRange(location:11,length:4))
lbl_Second.attributedText = myMutableString

Label- 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.red, 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.blue, range: NSRange(location:11,length:4))
lbl_Third.attributedText = myMutableString

Label-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.blue,range:  NSRange(location: 0,length: 1))
myMutableString.addAttribute(NSStrokeWidthAttributeName,value: 2,range: NSRange(location: 0,length: 1))
lbl_Forth.attributedText = myMutableString

Label -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.yellow, range: NSRange(location:0,length:myString.length))
lbl_Fifth.attributedText = myMutableString

Label-6: Change the background colour 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.red, range: NSRange(location:2,length:2))
myMutableString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.yellow, range: NSRange(location:2,length:2))

myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.green, range: NSRange(location:5,length:5))
myMutableString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.blue, range: NSRange(location:5,length:5))

myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blue, range: NSRange(location:11,length:4))
myMutableString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.orange, range: NSRange(location:11,length:4))

lbl_Six.attributedText = myMutableString

Demo Swift 3.0

Thanks, I will come with new topic.
How To Set Multiple Font Colors in a Single label in Swift 3.0? How To Set Multiple Font Colors in a Single label in Swift 3.0? Reviewed by KIRIT MODI on 23:14:00 Rating: 5

No comments:

Powered by Blogger.