Adjust letter spacing in iOS 7

前端 未结 3 1345
死守一世寂寞
死守一世寂寞 2020-12-24 01:22

In iOS 7, when navigating back using the new swipe-from-edge-of-screen gesture, the title of the Back button (\"Artists\") fades from being pink (in the example below) and h

3条回答
  •  温柔的废话
    2020-12-24 02:11

    With Swift 5.3 and iOS 14, NSAttributedString.Key has a static property called kern. kern has the following declaration:

    static let kern: NSAttributedString.Key
    

    The value of this attribute is an NSNumber object containing a floating-point value. This value specifies the number of points by which to adjust kern-pair characters. Kerning prevents unwanted space from occurring between specific characters and depends on the font. The value 0 means kerning is disabled. The default value for this attribute is 0.

    The following Playground code shows a possible implementation of kern in order to have some letter spacing in your NSAttributedString:

    import PlaygroundSupport
    import UIKit
    
    class ViewController: UIViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let string = "Some text"
            let paragraph = NSMutableParagraphStyle()
            paragraph.alignment = .center
            let attributes: [NSAttributedString.Key: Any] = [
                NSAttributedString.Key.kern: 10,
                NSAttributedString.Key.paragraphStyle: paragraph
            ]
            let attributedString = NSMutableAttributedString(
                string: string,
                attributes: attributes
            )
    
            let label = UILabel()
            label.attributedText = attributedString
    
            view.backgroundColor = .white
            view.addSubview(label)
    
            label.translatesAutoresizingMaskIntoConstraints = false
            NSLayoutConstraint.activate([
                label.centerXAnchor.constraint(equalTo: view.centerXAnchor),
                label.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            ])
        }
    }
    
    PlaygroundPage.current.liveView = ViewController()
    

提交回复
热议问题