How to change the distance between letters of UIButton in Swift?

I found how to set the distance between letters to UILabel ( here ), but this method does not work for UIButtons. Does anyone know how to do this?

Here is the code I'm using

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

It gives me an error: 'NSConcreteAttributedString' (0x19e508660) to 'NSMutableAttributedString' (0x19e506a40).

+8
source share
6 answers
  • Make NSAttributedString as in the question you linked
  • Call setAttributedTitle(_ ,forState:) on UIButton

Try this one (untested):

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

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) 
+13
source

Swift 4

 extension UIButton{ func addTextSpacing(_ letterSpacing: CGFloat){ let attributedString = NSMutableAttributedString(string: (self.titleLabel?.text!)!) attributedString.addAttribute(NSAttributedString.Key.kern, value: letterSpacing, range: NSRange(location: 0, length: (self.titleLabel?.text!.count)!)) self.setAttributedTitle(attributedString, for: .normal) } } // Usage: button.addTextSpacing(5.0) 
+5
source

Code Different does not support text color settings. You can also override the UIButton class to have an interval parameter that is available even in a storyboard. Here's an updated Swift 3 solution:

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_() } } } 
+1
source

Not a complete answer, but GOTCHA and FIX.

GOTCHA: Using character spacing also adds a space to the end of the text. This error / error means that the text aligned in the center will not display correctly.

FIX: when creating a Range for attribute text, subtract 1 from text.count (ignoring the last character in the string for spaces.)

e.g. incorrect centering due to extra space:

enter image description here

fixed:

enter image description here

[Edit]

As for the note, if you use EdgeInsets to indent text, your UIButton subclass UIButton have to override intrinsicContentsSize:

 override open var intrinsicContentSize: CGSize { let size = super.intrinsicContentSize let insets = self.titleEdgeInsets let width = size.width + insets.left + insets.right let height = size.height + insets.top + insets.bottom return CGSize(width: width, height: height) } 
+1
source

Update for Swift 4 based on jaya raj answer.

 extension UIButton{ func addTextSpacing(spacing: CGFloat){ let attributedString = NSMutableAttributedString(string: (self.titleLabel?.text!)!) attributedString.addAttribute(NSAttributedStringKey.kern, value: spacing, range: NSRange(location: 0, length: (self.titleLabel?.text!.characters.count)!)) self.setAttributedTitle(attributedString, for: .normal) } } extension UILabel{ func addTextSpacing(spacing: CGFloat){ let attributedString = NSMutableAttributedString(string: self.text!) attributedString.addAttribute(NSAttributedStringKey.kern, value: spacing, range: NSRange(location: 0, length: self.text!.characters.count)) self.attributedText = attributedString } } extension UITextField{ func addPlaceholderSpacing(spacing: CGFloat){ let attributedString = NSMutableAttributedString(string: self.placeholder!) attributedString.addAttribute(NSAttributedStringKey.kern, value: spacing, range: NSRange(location: 0, length: self.placeholder!.characters.count)) self.attributedPlaceholder = attributedString } } extension UINavigationItem{ func addSpacing(spacing: CGFloat){ let attributedString = NSMutableAttributedString(string: self.title!) attributedString.addAttribute(NSAttributedStringKey.kern, 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: UIFont.Weight.bold) label.attributedText = attributedString label.sizeToFit() self.titleView = label } } 
0
source

Source: https://habr.com/ru/post/1237270/


All Articles