swift - UITextView swift 中字符串的粗体部分

标签 swift uitextview

正文如下:

@IBOutlet weak var legalText: UITextView!
let textToAppend = "TERMS OF SERVICE\nLast Updated: May 7, 2015\n\nPLEASE NOTE: The HIGO Terms of Service as stated below is effective as of the 'Last Updated' date above for any user who is browsing the HIGO website, or for any user who creates a HIGO account on or after that date."
legalText.text = legalText.text.stringByAppendingString(textToAppend)

我想加粗“服务条款”和“请注意:下文所述的 HIGO 服务条款自上述“最后更新”日期起对浏览 HIGO 网站的任何用户或任何用户有效谁在该日期或之后创建了 HIGO 帐户。”

我尝试在 uitextview 中以编程方式使用 uilabel 但没有成功:

var termsofservice : UILabel = UILabel()
termsofservice.numberOfLines = 0
termsofservice.text = "TERMS OF SERVICE"
termsofservice.font = UIFont.boldSystemFontOfSize(20)
termsofservice.textAlignment = NSTextAlignment.Left;

var pleasenote : UILabel = UILabel()
pleasenote.numberOfLines = 0
pleasenote.text = "PLEASE NOTE: The HIGO Terms of Service as stated below is effective as of the 'Last Updated' date above for any user who is browsing the HIGO website, or for any user who creates a HIGO account on or after that date."
pleasenote.font = UIFont.boldSystemFontOfSize(20)
pleasenote.textAlignment = NSTextAlignment.Left;

let textToAppend = "\(termsofservice)\nLast Updated: May 7, 2015\n\n\(pleasenote)"

也试过这些但没有用,它只显示“服务条款”和“最后更新..”,没有显示“请注意...”

var termsofservice = "TERMS OF SERVICE"
var normalText = "\n\nLast Updated: May 7, 2015"
var pleasenote  = "\n\nPLEASE NOTE: The HIGO Terms of Service as stated below is effective as of the 'Last Updated' date above for any user who is browsing the HIGO website, or for any user who creates a HIGO account on or after that date."

var attributedString = NSMutableAttributedString(string:normalText)

var attrs = [NSFontAttributeName : UIFont.boldSystemFontOfSize(15)]
var boldString = NSMutableAttributedString(string:pleasenote, attributes:attrs)
var boldString0 = NSMutableAttributedString(string:termsofservice, attributes:attrs)

boldString0.appendAttributedString(attributedString)
attributedString.appendAttributedString(boldString)
legalText.attributedText = boldString0

如何把那部分字符串加粗?

注意:文本仍然很长,字符串的更多部分加粗。

最佳答案

针对 Swift 3 进行了更新

func attributedText() -> NSAttributedString {

    let string = "TERMS OF SERVICE\nLast Updated: May 7, 2015\n\nPLEASE NOTE: The HIGO Terms of Service as stated below is effective as of the 'Last Updated' date above for any user who is browsing the HIGO website, or for any user who creates a HIGO account on or after that date." as NSString

    let attributedString = NSMutableAttributedString(string: string as String, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 15.0)])

    let boldFontAttribute = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 15.0)]

    // Part of string to be bold
    attributedString.addAttributes(boldFontAttribute, range: string.range(of: "TERMS OF SERVICE"))
    attributedString.addAttributes(boldFontAttribute, range: string.range(of: "PLEASE NOTE:"))

    // 4
    return attributedString
}

并将 attributetext 文本设置为您的 TextView :

legalText.attributedText = attributedText()

关于swift - UITextView swift 中字符串的粗体部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31647653/

相关文章:

ios - Swift - 如何将帖子通知从 OneSignal SDK 发送到特定的用户名标签?

ios - 将点击从 UITextView 转发到 UITableViewCell

iphone - 具有两种不同字体颜色的 UILabel

ios - CocoaPods - 当应用程序和框架共享依赖项时重复符号

ios - 带有放大镜且没有键盘的 UITextView

ios - UITextView - 重新定位后开头的空白行

iphone - UItextView 阿拉伯文本右对齐

swift - 使用 CoreImage 生成条码 CI 39

ios - 多个原型(prototype)单元格到 UITableView

xcode - 从 child swift peformSegue