大家好,我正在尝试为我的 TextView 设置粗体和居中, View 中有 3 种不同的颜色,但我希望更改的 2 种颜色居中。有没有办法做到这一点?下面是我的代码。
let myString:String = DisplayableContents.texts[10]
var myMutableString = NSMutableAttributedString()
myMutableString = NSMutableAttributedString(string: myString, attributes: [NSFontAttributeName:UIFont(name: "Calibri", size: 17.0)!])
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSRange(location:74, length:96))
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.gray, range: NSRange(location:96, length:19))
// set label Attribute
infoBox.attributedText = myMutableString
最佳答案
尝试使用
let myString:String = DisplayableContents.texts[10]
var myMutableString = NSMutableAttributedString()
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.Center
myMutableString = NSMutableAttributedString(string: myString, attributes: [NSParagraphStyleAttributeName:paragraphStyle, NSFontAttributeName:UIFont(name: "Calibri", size: 17.0)!])
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSRange(location:74, length:96))
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.gray, range: NSRange(location:96, length:19))
// set label Attribute
infoBox.attributedText = myMutableString
但是 iOS 中不存在“Calibri”。您可以在 http://iosfonts.com/ 中查看字体。如果您要将字体添加到项目中,请检查其粗体变体,可能是“Calibri-Bold”
关于swift - 中心和粗体 TextView swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43032604/