How To Set A number of Font Colours in a Single label in Swift 3.0?

How To Set A number of Font Colours in a Single label in Swift 3.0?

In keeping with regular string in iOS, There’s not present the power of colourful string in iOS, however have a category NSAttributeString which have present colour of string facility. For formatting part of a string, you want an attributed string which specifies elements of the string formatted in numerous methods. In this lesson we’ll discover ways to work in attributed strings in Swift.

Initializing an Attributed String : initially initializing Attributed string, so including under code in ViewDidLoad technique.
var myString:NSString = "I AM KIRIT MODI"
var myMutableString = NSMutableAttributedString()
myMutableString
= NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(identify: "Georgia", measurement: 18.0)!])


Label-1: Change the color of single string.

Within the label an attributed string with the string myString and an attribute dictionary. Right here we used an attribute named NSForegroundColorAttributeName as the important thing, and a UIColor.redColor() object as the worth. and likewise passing vary of which wish to change NSRange(location:2,size:2) Construct and run. Code as under of first label.

myMutableString.addAttribute(NSForegroundColorAttributeName, worth: UIColor.pink, vary: NSRange(location:2,size:2))
lbl_First
.attributedText = myMutableString

Label-2: Change the colour of extra string.

Identical observe as above steps however including right here three attribute NSForegroundColorAttributeName with three totally different colour identify and totally different vary worth, Code as under of second label.
myMutableString = NSMutableAttributedString()
myMutableString
= NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(identify: "Georgia", measurement: 18.0)!])
myMutableString
.addAttribute(NSForegroundColorAttributeName, worth: UIColor.pink, vary: NSRange(location:2,size:2))
myMutableString
.addAttribute(NSForegroundColorAttributeName, worth: UIColor.inexperienced, vary: NSRange(location:5,size:5))
myMutableString
.addAttribute(NSForegroundColorAttributeName, worth: UIColor.blue, vary: NSRange(location:11,size:4))
lbl_Second
.attributedText = myMutableString

Label- 3: Change the colour and font of string.

we used an attribute named NSFontAttributeName as the important thing, and a UIFont(identify: “AmericanTypewriter-Daring”, measurement: 18.0)! object as the worth. and likewise passing vary of which wish to changeNSRange(location:5,size:5)  Construct and run. Code as under of first label.

myMutableString = NSMutableAttributedString()
myMutableString
= NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(identify: "Georgia", measurement: 18.0)!])
myMutableString
.addAttribute(NSForegroundColorAttributeName, worth: UIColor.pink, vary: NSRange(location:2,size:2))
myMutableString
.addAttribute(NSFontAttributeName, worth: UIFont(identify: "AmericanTypewriter-Daring", measurement: 18.0)!, vary: NSRange(location:5,size:5))
myMutableString
.addAttribute(NSForegroundColorAttributeName, worth: UIColor.blue, vary: NSRange(location:11,size:4))
lbl_Third
.attributedText = myMutableString

Label-4: Change the Stroke colour and width of string.

we used two attribute named NSStrokeColorAttributeName as the important thing, and a UIColor.blueColor() object as the worth. NSStrokeWidthAttributeName as the important thing, and a “2” object as the worth. and provides the identical vary each including attribute.  Construct and run. Code as under forth label.

myMutableString = NSMutableAttributedString()
myMutableString
= NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(identify: "Georgia", measurement: 18.0)!])
myMutableString
.addAttribute(NSFontAttributeName,
worth
: UIFont(identify: "Georgia",measurement: 36.0)!,vary: NSRange(location: 0,size: 1))
myMutableString
.addAttribute(NSStrokeColorAttributeName,worth: UIColor.blue,vary: NSRange(location: 0,size: 1))
myMutableString
.addAttribute(NSStrokeWidthAttributeName,worth: 2,vary: NSRange(location: 0,size: 1))
lbl_Forth
.attributedText = myMutableString

Label -5 : Change the background colour of string.

we used attribute named NSBackgroundColorAttributeName as the important thing, and a  UIColor.yellowColor() object as the worth. and vary with full string size, Construct and run. Code as under fifth label.

myMutableString = NSMutableAttributedString()
myMutableString
= NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(identify: "Georgia", measurement: 18.0)!])
myMutableString
.addAttribute(NSBackgroundColorAttributeName, worth: UIColor.yellow, vary: NSRange(location:0,size:myString.size))
lbl_Fifth
.attributedText = myMutableString

Label-6: Change the background color of  particular person string.

we used attribute named NSBackgroundColorAttributeName property for particular person vary, and alter background colour in line with vary, Code as under sixth label.
myMutableString = NSMutableAttributedString()
myMutableString
= NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(identify: "Georgia", measurement: 18.0)!])
myMutableString
.addAttribute(NSForegroundColorAttributeName, worth: UIColor.pink, vary: NSRange(location:2,size:2))
myMutableString
.addAttribute(NSBackgroundColorAttributeName, worth: UIColor.yellow, vary: NSRange(location:2,size:2))

myMutableString
.addAttribute(NSForegroundColorAttributeName, worth: UIColor.inexperienced, vary: NSRange(location:5,size:5))
myMutableString
.addAttribute(NSBackgroundColorAttributeName, worth: UIColor.blue, vary: NSRange(location:5,size:5))

myMutableString
.addAttribute(NSForegroundColorAttributeName, worth: UIColor.blue, vary: NSRange(location:11,size:4))
myMutableString
.addAttribute(NSBackgroundColorAttributeName, worth: UIColor.orange, vary: NSRange(location:11,size:4))

lbl_Six
.attributedText = myMutableString

Demo Swift 3.0

Thanks, I’ll include new subject.

Tags:

Related Posts

Leave a Reply