You probably don't need an “extension” as such if you want this for any custom fonts ...
Just add a function (similar to the part above) that you can call from anywhere (i.e. without a class), and then you can repeatedly spoof words on any line, calling only ONE LINE of code:
To go to a file like constantants.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 }
Then you can simply call this one line of code for any UILabel:
self.UILabel.attributedText = addBoldText("Check again in 30 DAYS to find more friends", boldPartOfString: "30 DAYS", font: normalFont!, boldFont: boldSearchFont!) //Mark: Albeit that you've had to define these somewhere: let normalFont = UIFont(name: "INSERT FONT NAME", size: 15) let boldFont = UIFont(name: "INSERT BOLD FONT", size: 15)
David West May 21 '16 at 1:13 2016-05-21 01:13
source share