How to set kerning in iPhone UILabel

后端 未结 9 1203
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:47

    Swift 4 and 5

    extension NSAttributedString {
    
        /// Returns a new instance of NSAttributedString with same contents and attributes with kerning added.
        /// - Parameter kerning: a kerning you want to assign to the text.
        /// - Returns: a new instance of NSAttributedString with given kerning.
        func withKerning(_ kerning: CGFloat) -> NSAttributedString {
            let attributedString = NSMutableAttributedString(attributedString: self)
            attributedString.addAttributes([.kern: kerning],
                                           range: NSRange(location: 0, length: string.count))
            return NSAttributedString(attributedString: attributedString)
        }
     ]
    

提交回复
热议问题