2015-12-02 14 views
5

Ho trovato come impostare la spaziatura delle lettere su UILabel (here) ma questo metodo non funziona per UIButtons. Qualcuno sa come farlo?Come modificare la spaziatura tra lettere di UIButton in Swift?

Ecco il codice che sto utilizzando

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

Mi genera un errore: 'NSConcreteAttributedString' (0x19e508660) to 'NSMutableAttributedString' (0x19e506a40).

+0

Dove è esattamente il problema? Per favore pubblica il codice che non funziona per te. –

+0

Aggiornato per voi per vedere il codice rotto. –

risposta

10
  1. Fai il NSAttributedString come nella questione si è collegato
  2. chiamata setAttributedTitle(_ ,forState:) sul tuo UIButton

Prova questa (non testata):

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

Fatto. Ho aggiornato la domanda con codice ed errore. –

+0

No, errore: valore di tipo 'UIButton' non ha membro 'setAttributedTitleForState' –

+0

Il mio male. Dovrebbe essere stato solo "setAttributedTitle". Modificato –

2

La soluzione da Code Different non rispetta le impostazioni colore del testo. Inoltre è possibile sovrascrivere la classe UIButton per avere il parametro di spaziatura disponibile anche nello storyboard. Ecco che arriva uno Swift 3 soluzione di aggiornamento:

Swift 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

Swift 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) 
Problemi correlati