Ссылка отсюда .
Прежде всего инициализируйте NSString и NSMutableAttributedString, как показано ниже.
var myString:NSString = "I AM KIRIT MODI"
var myMutableString = NSMutableAttributedString()
В 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()
}
ВЫХОД
НЕСКОЛЬКО ЦВЕТОВ
Добавьте приведенный ниже код строки в свой ViewDidLoad, чтобы получить несколько цветов в строке.
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.greenColor(), range: NSRange(location:10,length:5))
Многоцветный ВЫХОД
Swift 4
var myMutableString = NSMutableAttributedString(string: str, attributes: [NSAttributedStringKey.font :UIFont(name: "Georgia", size: 18.0)!])
myMutableString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red, range: NSRange(location:2,length:4))
Swift 5.0
var myMutableString = NSMutableAttributedString(string: str, attributes: [NSAttributedString.Key.font :UIFont(name: "Georgia", size: 18.0)!])
myMutableString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red, range: NSRange(location:2,length:4))
person
Kirit Modi
schedule
01.01.2015