UITextView swift中字符串的粗体部分

Sar*_*min 19 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)
Run Code Online (Sandbox Code Playgroud)

我想大胆的"服务条款"和"请注意:海高服务条款的规定以下是有效如上对于谁是浏览海高的网站,或任何用户谁创建的任何用户的'最近更新’日期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)"
Run Code Online (Sandbox Code Playgroud)

也尝试这些但没有工作,它只显示"服务条款"和"最后更新..",没有显示"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
Run Code Online (Sandbox Code Playgroud)

如何加粗那部分字符串?

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

Ham*_*ari 45

针对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
}
Run Code Online (Sandbox Code Playgroud)

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

legalText.attributedText = attributedText()
Run Code Online (Sandbox Code Playgroud)

  • 如果你将使用字符串作为NSString`rangeOfString`将返回NSRange,希望它清除你的怀疑. (2认同)

Dav*_*est 8

更好的是,你可以继续在你的军械库中使用这个1函数,在像Constants.swift这样的文件中然后你可以通过调用一行代码在很多情况下在任何字符串中加入单词:

要进入没有类的文件,比如我的情况下的constants.swift:

import Foundation
import UIKit

func addBoldText(fullString: NSString, boldPartOfString: NSString, font: UIFont!, boldFont: UIFont!) -> NSAttributedString {
    let nonBoldFontAttribute = [NSAttributedString.Key.font:font!]
    let boldFontAttribute = [NSAttributedString.Key.font:boldFont!]
    let boldString = NSMutableAttributedString(string: fullString as String, attributes:nonBoldFontAttribute)
    boldString.addAttributes(boldFontAttribute, range: fullString.range(of: boldPartOfString as String))
    return boldString
}
Run Code Online (Sandbox Code Playgroud)

然后你可以为任何UILabel调用这一行代码:

let normalFont = UIFont(name: "INSERT FONT NAME", size: 14)
let boldFont = UIFont(name: "INSERT BOLD FONT NAME", size: 14)
self.UILabel.attributedText = addBoldText("Check again in 30 DAYS to find more friends", boldPartOfString: "30 DAYS", font: normalFont!, boldFont: boldFont!)
Run Code Online (Sandbox Code Playgroud)