개발/iOS

swift uilabel multi color, font

도래울 2016. 5. 23. 17:10

First of all initialize of you NSString and NSMutableAttributedString as below.

var myString:NSString = "I AM KIRIT MODI"
var myMutableString = NSMutableAttributedString()

In ViewDidLoad

override func viewDidLoad() {


        myMutableString = NSMutableAttributedString(string: myString, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])

        myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location:2,length:4))

       // set label Attribute

        labName.attributedText = myMutableString

        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

You OUTPUT :

enter image description here

MULTIPLE COLOR

Add in you ViewDidLoad below line code to get multiple color in string.

 myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.greenColor(), range: NSRange(location:10,length:5))

Mupltiple color OUTPUT

enter image description here