bold part of string in UITextView swift
Updated for 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()
Better still, you can go ahead and use this 1 function in your armoury, in a file like Constants.swift and then you can embolden words within any string, on numerous occasions by calling just ONE LINE of code:
To go in a file without a class, like constants.swift in my case:
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
}
Then you can just call this one line of code for any 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!)
Swift 3 Update (from the answer of @Hamza Ansari)
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
}
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..<fullString.endIndex, in: fullString))
//4
return attributedString
}