bold part of string in UITextView swift

前端 未结 4 1385
心在旅途
心在旅途 2020-12-14 09:15

Here is the text:

@IBOutlet weak var legalText: UITextView!
let textToAppend = \"TERMS OF SERVICE\\nLast Updated: May 7, 2015\\n\\nPLEASE NOTE: The HIGO Term         


        
4条回答
  •  爱一瞬间的悲伤
    2020-12-14 09:38

    Swift 4.2 Update

    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..

提交回复
热议问题