How do you adjust text kerning using Interface Builder in Xcode 7?

后端 未结 5 1745
無奈伤痛
無奈伤痛 2021-02-13 18:33

There are a myriad of settings for NSAttributedParagraphStyle that I can see in Interface Builder:

5条回答
  •  野的像风
    2021-02-13 18:34

    You can actually do this without the use of a subclass through an extension.

    import UIKit
    
    @IBDesignable
    extension UILabel {
        @IBInspectable
        public var kerning:CGFloat {
            set{
                if let currentAttibutedText = self.attributedText {
                    let attribString = NSMutableAttributedString(attributedString: currentAttibutedText)
                    attribString.addAttributes([NSKernAttributeName:newValue], range:NSMakeRange(0, currentAttibutedText.length))
                    self.attributedText = attribString
                }
            } get {
                var kerning:CGFloat = 0
                if let attributedText = self.attributedText {
                    attributedText.enumerateAttribute(NSKernAttributeName,
                                                      in: NSMakeRange(0, attributedText.length),
                                                      options: .init(rawValue: 0)) { (value, range, stop) in
                                                        kerning = value as? CGFloat ?? 0
                    }
                }
                return kerning
            }
        }
    }
    

    While this won't actually show up in interface builder it will show up and work when you run your app.

提交回复
热议问题