Change character spacing on UILabel within Interface Builder

前端 未结 13 2631
说谎
说谎 2020-12-23 09:21

Is there anyway to change the character spacing (track) on UILabel text using Interface Builder? If not, is there a way to do it programmatically on an existing UILabel that

13条回答
  •  再見小時候
    2020-12-23 09:43

    Ended up using this for now to get existing attributed text and modify to add character spacing:

    let attributedString = discoveryTitle.attributedText as NSMutableAttributedString
    attributedString.addAttribute(NSKernAttributeName, value: 1.0, range: NSMakeRange(0, attributedString.length))
    discoveryTitle.attributedText = attributedString
    

    Swift 3:

    let attributedString = NSMutableAttributedString(string: discoveryTitle.text)
    attributedString.addAttribute(NSKernAttributeName, value: CGFloat(1.0), range: NSRange(location: 0, length: attributedString.length))
    discoveryTitle.attributedText = attributedString
    

    Using NSRange instead of NSMakeRange works in Swift 3.

提交回复
热议问题