2015-12-02 97 views
5

我發現如何設置字母間距到UILabel(here),但此方法不適用於UIButtons。有誰知道該怎麼做?如何在Swift中更改UIButton的字母間距?

這裏是我使用

let buttonString = agreementButton.attributedTitleForState(.Normal) as! NSMutableAttributedString 
    buttonString.addAttribute(NSKernAttributeName, value: 1.0, range: NSMakeRange(0, buttonString.length)) 
    agreementButton.setAttributedTitle(buttonString, forState: .Normal) 

這引發了我的錯誤代碼:'NSConcreteAttributedString' (0x19e508660) to 'NSMutableAttributedString' (0x19e506a40).

+0

確切位置在哪裏出了問題?請發佈不適合你的代碼。 –

+0

更新爲你看破碎的代碼。 –

回答

10
  1. 充分利用NSAttributedString如你問題你對你的UIButton
  2. 鏈接
  3. 呼叫setAttributedTitle(_ ,forState:)

試試這個(未經測試):

let title = agreementButton.titleForState(.Normal) 
let attributedTitle = NSAttributedString(string: title, attributes: [NSKernAttributeName: 1.0]) 
agreementButton.setAttributedTitle(attributedTitle, forState: .Normal) 
+0

完成。我用代碼和錯誤更新了這個問題。 –

+0

不,錯誤:類型'UIButton'的值沒有成員'setAttributedTitleForState' –

+0

我的不好。應該只是'setAttributedTitle'。編輯 –

2

Code Different該解決方案不尊重文本顏色設置。也可以重寫UIButton類,使間隔參數即使在故事板中也可用。這裏談到的更新斯威夫特3溶液:

斯威夫特3

class UIButtonWithSpacing : UIButton 
{ 
    override func setTitle(_ title: String?, for state: UIControlState) 
    { 
     if let title = title, spacing != 0 
     { 
      let color = super.titleColor(for: state) ?? UIColor.black 
      let attributedTitle = NSAttributedString(
       string: title, 
       attributes: [NSKernAttributeName: spacing, 
          NSForegroundColorAttributeName: color]) 
      super.setAttributedTitle(attributedTitle, for: state) 
     } 
     else 
     { 
      super.setTitle(title, for: state) 
     } 
    } 

    fileprivate func updateTitleLabel_() 
    { 
     let states:[UIControlState] = [.normal, .highlighted, .selected, .disabled] 
     for state in states 
     { 
      let currentText = super.title(for: state) 
      self.setTitle(currentText, for: state) 
     } 
    } 

    @IBInspectable var spacing:CGFloat = 0 { 
     didSet { 
      updateTitleLabel_() 
     } 
    } 
} 
6

雨燕3.0

extension UIButton{ 
    func addTextSpacing(spacing: CGFloat){ 
     let attributedString = NSMutableAttributedString(string: (self.titleLabel?.text!)!) 
     attributedString.addAttribute(NSKernAttributeName, value: spacing, range: NSRange(location: 0, length: (self.titleLabel?.text!.characters.count)!)) 
     self.setAttributedTitle(attributedString, for: .normal) 
    } 
} 
btnRegister.addTextSpacing(spacing: 4.5) 

extension UILabel{ 
    func addTextSpacing(spacing: CGFloat){ 
     let attributedString = NSMutableAttributedString(string: self.text!) 
     attributedString.addAttribute(NSKernAttributeName, value: spacing, range: NSRange(location: 0, length: self.text!.characters.count)) 
     self.attributedText = attributedString 
    } 
} 
lblOne.addTextSpacing(spacing: 4.5) 

extension UITextField{ 
    func addPlaceholderSpacing(spacing: CGFloat){ 
     let attributedString = NSMutableAttributedString(string: self.placeholder!) 
     attributedString.addAttribute(NSKernAttributeName, value: spacing, range: NSRange(location: 0, length: self.placeholder!.characters.count)) 
     self.attributedPlaceholder = attributedString 
    } 
} 
txtUserName.addPlaceholderSpacing(spacing: 4.5) 

extension UINavigationItem{ 
    func addSpacing(spacing: CGFloat){ 
     let attributedString = NSMutableAttributedString(string: self.title!) 
     attributedString.addAttribute(NSKernAttributeName, value: spacing, range: NSRange(location: 0, length: self.title!.characters.count)) 
     let label = UILabel() 
     label.textColor = UIColor.black 
     label.font = UIFont.systemFont(ofSize: 15, weight: UIFontWeightBold) 
     label.attributedText = attributedString 
     label.sizeToFit() 
     self.titleView = label 
    } 
} 
navigationItem.addSpacing(spacing: 2.5)