Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

But none of these are for text kerning. Is there a way to adjust the text kerning in Xcode 7's Interface Builder for attributed text?

(Please don't answer with how to do this in code - I already know how to do that!)

like image 661
brandonscript Avatar asked Sep 28 '15 16:09

brandonscript


Video Answer


1 Answers

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
        }
    }
}

enter image description here

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

like image 163
robertwalsh Avatar answered Sep 27 '22 00:09

robertwalsh