HomeiOS DevelopmentThe best way to create NSMutableAttributedString in swift – iOSTutorialJunction

The best way to create NSMutableAttributedString in swift – iOSTutorialJunction


The best way to create NSMutableAttributedString in swift – iOSTutorialJunction

Introduction to NSMutableAttributedString in swift:

NSMutableAttributedString is an effective way to customise or embellish UILable textual content programmatically. For instance your requirement includes sure phrase in the midst of the textual content is in daring and in numerous colour, what’s going to you do. On this case one can use NSMutableAttributedString class supplied in swift language. On this publish we are going to study completely different attributes of NSMutableAttributedString .

Implementation of code:

Allow us to begin by creating an new undertaking by selecting single view utility template and choose growth language as SWIFT/swift3, title your undertaking NSMutableAttributedString-Tutorial.

Open ViewController.swift class create your IBOutlet for UILable

    @IBOutlet var strLable : UILabel?

Open essential.storyboard and drag and UILable to it. Join your strLable to UILable dragged on to the view.

Open ViewController.swift file and inside our viewDidLoad methodology we are going to create our NSMutableAttributedString.

  
 let stringAttributed = NSMutableAttributedString.init(string: “If have already got  account, please Login”)

     

Within the above string we are going to spotlight “Login”by making it daring and inexperienced colour string.To realize this we have now so as to add attribute to our attributed string. Assign out NSMutableAttributedString to our desk.

  

let font = UIFont(title: “verdana-bold”, measurement: 18.0)

stringAttributed.addAttribute(NSFontAttributeName, worth:font!, vary: NSRange.init(location: 32, size: 5))

        stringAttributed.addAttribute(NSForegroundColorAttributeName, worth: UIColor.inexperienced, vary: NSRange.init(location: 32, size: 5))

 

  strLable?.attributedText = stringAttributed;

Run your code, you will note output just like the under picture

NSMutableAttributedString_Swift_tutorial

 

Underline Attribute:

let font = UIFont(title: “verdana-bold”, measurement: 18.0)

        stringAttributed.addAttribute(NSFontAttributeName, worth:font!, vary: NSRange.init(location: 32, size: 5))

        stringAttributed.addAttribute(NSForegroundColorAttributeName, worth: UIColor.inexperienced, vary: NSRange.init(location: 32, size: 5))

        stringAttributed.addAttribute(NSUnderlineStyleAttributeName, worth: 1.0, vary: NSRange.init(location: 32, size: 5))

strLable?.attributedText = stringAttributed;

Run your code, you will note output just like the under picture

NSMutableAttributedString_Swift_Tutorial_underlineStyle

 

Background colour Attribute:

let font = UIFont(title: “verdana-bold”, measurement: 18.0)

        stringAttributed.addAttribute(NSFontAttributeName, worth:font!, vary: NSRange.init(location: 32, size: 5))

        stringAttributed.addAttribute(NSForegroundColorAttributeName, worth: UIColor.inexperienced, vary: NSRange.init(location: 32, size: 5))

        stringAttributed.addAttribute(NSBackgroundColorAttributeName, worth: UIColor.yellow, vary: NSRange.init(location: 0, size: stringAttributed.size))

        strLable?.attributedText = stringAttributed;

Run your code, you will note output just like the under picture

 

NSMutableAttributedString_Swift_Tutorial_BackgroundColorAttribute

 

Stroke colour Attribute and stroke width attribute:

 let font = UIFont(title: “verdana-bold”, measurement: 18.0)

        stringAttributed.addAttribute(NSFontAttributeName, worth:font!, vary: NSRange.init(location: 32, size: 5))

        stringAttributed.addAttribute(NSForegroundColorAttributeName, worth: UIColor.inexperienced, vary: NSRange.init(location: 32, size: 5))

        stringAttributed.addAttribute(NSStrokeColorAttributeName, worth: UIColor.crimson, vary: NSRange.init(location: 0, size: stringAttributed.size))

        stringAttributed.addAttribute(NSStrokeWidthAttributeName, worth: 2.0, vary: NSRange.init(location: 0, size: stringAttributed.size))

        strLable?.attributedText = stringAttributed;

Run your code, you will note output just like the under picture

 

NSMutableAttributedString_Swift_Tutorial_storeColorattribute_strokeWidthAttribute

Final feedback:

In above tutorial we discovered  a number of the attributes supplied by NSMutableAttributedString in swift language. Hope this helps you win studying NSMutableAttributedString in swift and in adorning your textual content headlines within the iOS app. Keep tuned for extra tutorials on swift. 



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments