How to set kerning in iPhone UILabel

后端 未结 9 1202
Happy的楠姐
Happy的楠姐 2020-11-30 01:12

I am developing an iPhone app, and I want to set kerning in UILabel. The code I\'ve written (possibly around kCTKernAttributeName) seems to be in error. How mig

9条回答
  •  我在风中等你
    2020-11-30 01:48

    In Swift 2.0...

    Add an extension:

    extension UIView {
        func attributes(font: String, color: UIColor, fontSize: CGFloat, kern: Double) -> [String: NSObject] {
            let attribute = [
                NSForegroundColorAttributeName: color,
                NSKernAttributeName: kern,
                NSFontAttributeName : UIFont(name: font, size: fontSize)!
            ]
            return attribute
        }
    }
    

    Now, just set your UILabel as attributedText:

    self.label.attributedText = NSMutableAttributedString(string: "SwiftExample", attributes: attributes("SourceSans-Regular", color: UIColor.whiteColor(), fontSize: 20, kern: 2.0))   
    

    Obviously, I added a bunch of parameters that you may not need. Play around -- feel free to rewrite the method -- I was looking for this on a bunch of different answers so figured I'd post the whole extension in case it helps someone out there... -rab

提交回复
热议问题