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

后端 未结 5 1743
無奈伤痛
無奈伤痛 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:37

    Swift 5

    UILabel Extension:

    @IBDesignable
    extension UILabel {
        @IBInspectable
          var letterSpace: CGFloat {
              set {
                  let attributedString: NSMutableAttributedString!
                  if let currentAttrString = attributedTitle(for: .normal) {
                      attributedString = NSMutableAttributedString(attributedString: currentAttrString)
                  }
                  else {
                      attributedString = NSMutableAttributedString(string: self.titleLabel?.text ?? "")
                      setTitle(.none, for: .normal)
                  }
    
                  attributedString.addAttribute(NSKernAttributeName,
                                                 value: newValue,
                                                 range: NSRange(location: 0, length: attributedString.length))
    
                  setAttributedTitle(attributedString, for: .normal)
              }
    
              get {
                  if let currentLetterSpace = attributedTitle(for: .normal)?.attribute(NSKernAttributeName, at: 0, effectiveRange: .none) as? CGFloat {
                      return currentLetterSpace
                  }
                  else {
                      return 0
                  }
              }
          }
    }
    

    UIButton Extension

        extension UIButton{
              @IBInspectable
              var letterSpace: CGFloat {
                  set {
                      let attributedString: NSMutableAttributedString!
                      if let currentAttrString = attributedTitle(for: .normal) {
                          attributedString = NSMutableAttributedString(attributedString: currentAttrString)
                      }
                      else {
                          attributedString = NSMutableAttributedString(string: self.titleLabel?.text ?? "")
                          setTitle(.none, for: .normal)
                      }
        
                    attributedString.addAttribute(NSAttributedString.Key.kern,
                                                     value: newValue,
                                                     range: NSRange(location: 0, length: attributedString.length))
        
                      setAttributedTitle(attributedString, for: .normal)
                  }
        
                  get {
                    if let currentLetterSpace = attributedTitle(for: .normal)?.attribute(NSAttributedString.Key.kern, at: 0, effectiveRange: .none) as? CGFloat {
                          return currentLetterSpace
                      }
                      else {
                          return 0
                      }
                  }
              }
    }
    

提交回复
热议问题