一尘不染

UITextView中字符串的粗体部分

swift

这是文本:

@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网站的任何用户生效。
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

如何加粗字符串的那一部分?

注意:文本仍然很长,并且字符串的更多部分变为粗体。


阅读 380

收藏
2020-07-07

共1个答案

一尘不染

为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
}

并将attributext文本设置为以下文本视图:

legalText.attributedText = attributedText()
2020-07-07