How to increase the character spacing in UILabel

后端 未结 6 754
孤独总比滥情好
孤独总比滥情好 2020-12-23 21:23

I am creating an app in >=iOS6. And I want to change the character spacing in UILabel. I have added the custom font \"FUTURABT HEAVY\" in my app but the Character are too cl

6条回答
  •  悲&欢浪女
    2020-12-23 21:58

    Swift 4.2 with UILabel extension and @IBInspectable

    extension UILabel {
    @IBInspectable var letterSpacing: CGFloat {
        get {
            var range:NSRange = NSMakeRange(0, self.text?.count ?? 0)
            let nr = self.attributedText?.attribute(NSAttributedString.Key.kern, at: 0, effectiveRange: &range) as! NSNumber
            return CGFloat(truncating: nr)
        }
    
        set {
            let range:NSRange = NSMakeRange(0, self.text?.count ?? 0)
    
            let attributedString = NSMutableAttributedString(string: self.text ?? "")
            attributedString.addAttribute(NSAttributedString.Key.kern, value: newValue, range: range)
            self.attributedText = attributedString
        }
    }
    

    }

提交回复
热议问题