UITextView swift中字符串的粗体部分

时间:2015-07-27 07:51:29

标签: 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

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

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

4 个答案:

答案 0 :(得分:42)

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

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

legalText.attributedText = attributedText()

答案 1 :(得分:6)

更好的是,你可以继续在你的军械库中使用这个1函数,在像Constants.swift这样的文件中然后你可以通过调用 ONE LINE

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

import Foundation
import UIKit

func addBoldText(fullString: NSString, boldPartOfString: NSString, font: UIFont!, boldFont: UIFont!) -> NSAttributedString {
   let nonBoldFontAttribute = [NSFontAttributeName:font!]
   let boldFontAttribute = [NSFontAttributeName:boldFont!]
   let boldString = NSMutableAttributedString(string: fullString as String, attributes:nonBoldFontAttribute)
   boldString.addAttributes(boldFontAttribute, range: fullString.rangeOfString(boldPartOfString as String))
   return boldString
}

然后你可以为任何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!)

答案 2 :(得分:1)

Swift 3更新(来自@Hamza Ansari的回答)

{{filtered.length}}

答案 3 :(得分:0)

Swift 4.2更新

func addBoldText(fullString: String, boldPartOfString: String, baseFont: UIFont, boldFont: UIFont) -> NSAttributedString {

    //1
    let baseFontAttribute = [NSAttributedString.Key.font : baseFont]
    let boldFontAttribute = [NSAttributedString.Key.font : boldFont]

    //2
    let attributedString = NSMutableAttributedString(string: fullString, attributes: baseFontAttribute)

    //3. Note if 'boldPartOfString' is not a substring of 'fullString', enitre 'fullString' will be formatted with 'boldFont'
    attributedString.addAttributes(boldFontAttribute, range: NSRange(fullString.range(of: boldPartOfString) ?? fullString.startIndex..<fullString.endIndex, in: fullString))

    //4
    return attributedString
}