UITextView swift中字符串的粗体部分 [英] bold part of string in UITextView swift

查看:33
本文介绍了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 帐户."

i want to bold "TERMS OF SERVICE" and "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."

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

i try with use uilabel programmatically in uitextview but did not work :

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)"

也尝试过这些但没有用,它只显示服务条款"和上次更新..",没有显示请注意..."

also try with these but did not work, it only show "TERMS OF SERVICE" and "last update..", did not show "PLEASE NOTE..."

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

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

How to bold that part of string?

注意:文本仍然很长,并且有更多的字符串部分要加粗.

Note:the text is still long and have more part of string to bold.

推荐答案

已针对 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
}

并将属性文本设置为您的文本视图:

And set attributext text to your text view as:

legalText.attributedText = attributedText()

这篇关于UITextView swift中字符串的粗体部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆