使用NSMutableAttributedString
原文链接:https://www.f2er.com/swift/320312.html小例子
let yourAttributes = [NSForegroundColorAttributeName: UIColor.blackColor(),NSFontAttributeName: UIFont.systemFontOfSize(15)] let yourOtherAttributes = [NSForegroundColorAttributeName: UIColor.redColor(),NSFontAttributeName: UIFont.systemFontOfSize(25)] let partOne = NSMutableAttributedString(string: "This is an example ",attributes: yourAttributes) let partTwo = NSMutableAttributedString(string: "for the combination of Attributed String!",attributes: yourOtherAttributes) let combination = NSMutableAttributedString() combination.appendAttributedString(partOne) combination.appendAttributedString(partTwo)
Swift 3
combination.append(partOne) combination.append(partTwo)
然后,组合是您的最终字符串,其中包含由您的属性和yourOtherAttributes提供的格式