도래울

swift uilabel multi color, font 본문

개발/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

'개발 > iOS' 카테고리의 다른 글

swift uitableview cell insert delete crash  (0) 2016.06.07
swift for문 index알기  (0) 2016.05.26
swift alert ios8 example  (0) 2016.05.23
UITextField 에서 Money Format 자동완성 하기  (0) 2016.05.23
Swift image viewer 오픈소스  (0) 2016.05.10
Comments