How to create NSMutableAttributedString in swift

NSMutableAttributedString_Swift_Tutorial_swift3.0

Introduction to NSMutableAttributedString in swift:

NSMutableAttributedString is a great way to customize or decorate UILable text programmatically. For example your requirement involves certain word in the middle of the text is in bold and in different color, what will you do. In this case one can use NSMutableAttributedString class provided in swift language. In this post we will learn different attributes of NSMutableAttributedString .

Implementation of code:

Let us start by creating an new project by choosing single view application template and select development language as SWIFT/swift3, name your project NSMutableAttributedString-Tutorial.

Open ViewController.swift class create your IBOutlet for UILable

    @IBOutlet var strLable : UILabel?

Open main.storyboard and drag and UILable to it. Connect your strLable to UILable dragged on to the view.

Open ViewController.swift file and inside our viewDidLoad method we will create our NSMutableAttributedString.

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

     

In the above string we will highlight “Login”by making it bold and green color string.To achieve this we have to add attribute to our attributed string. Assign out NSMutableAttributedString to our table.

  
let font = UIFont(name: “verdana-bold”, size: 18.0)
stringAttributed.addAttribute(NSFontAttributeName, value:font!, range: NSRange.init(location: 32, length: 5))
        stringAttributed.addAttribute(NSForegroundColorAttributeName, value: UIColor.green, range: NSRange.init(location: 32, length: 5))

 

  strLable?.attributedText = stringAttributed;

Run your code, you will see output like the below image

NSMutableAttributedString_Swift_tutorial

 

Underline Attribute:
let font = UIFont(name: “verdana-bold”, size: 18.0)
        stringAttributed.addAttribute(NSFontAttributeName, value:font!, range: NSRange.init(location: 32, length: 5))
        stringAttributed.addAttribute(NSForegroundColorAttributeName, value: UIColor.green, range: NSRange.init(location: 32, length: 5))
        stringAttributed.addAttribute(NSUnderlineStyleAttributeName, value: 1.0, range: NSRange.init(location: 32, length: 5))
strLable?.attributedText = stringAttributed;

Run your code, you will see output like the below image

NSMutableAttributedString_Swift_Tutorial_underlineStyle
 
Background color Attribute:
let font = UIFont(name: “verdana-bold”, size: 18.0)
        stringAttributed.addAttribute(NSFontAttributeName, value:font!, range: NSRange.init(location: 32, length: 5))
        stringAttributed.addAttribute(NSForegroundColorAttributeName, value: UIColor.green, range: NSRange.init(location: 32, length: 5))
        stringAttributed.addAttribute(NSBackgroundColorAttributeName, value: UIColor.yellow, range: NSRange.init(location: 0, length: stringAttributed.length))
        strLable?.attributedText = stringAttributed;
 
Run your code, you will see output like the below image
 
NSMutableAttributedString_Swift_Tutorial_BackgroundColorAttribute
 
Stroke color Attribute and stroke width attribute:
 let font = UIFont(name: “verdana-bold”, size: 18.0)
        stringAttributed.addAttribute(NSFontAttributeName, value:font!, range: NSRange.init(location: 32, length: 5))
        stringAttributed.addAttribute(NSForegroundColorAttributeName, value: UIColor.green, range: NSRange.init(location: 32, length: 5))
        stringAttributed.addAttribute(NSStrokeColorAttributeName, value: UIColor.red, range: NSRange.init(location: 0, length: stringAttributed.length))
        stringAttributed.addAttribute(NSStrokeWidthAttributeName, value: 2.0, range: NSRange.init(location: 0, length: stringAttributed.length))
        strLable?.attributedText = stringAttributed;
 
Run your code, you will see output like the below image
 
NSMutableAttributedString_Swift_Tutorial_storeColorattribute_strokeWidthAttribute

Final comments:

In above tutorial we learned  some of the attributes provided by NSMutableAttributedString in swift language. Hope this helps you win learning NSMutableAttributedString in swift and in decorating your text headlines in the iOS app. Stay tuned for more tutorials on swift. 

1 thought on “How to create NSMutableAttributedString in swift”

  1. Pingback: NSMutableAttributedString iOS Tutorial - iOSTutorialJunction

Comments are closed.