How to change the distance between letters in Swift?

I would like to change the distance between the letters in the title bar of my navigation bar, but so far I have not had success.

Thanks in advance.

+5
source share
3 answers

You can set the title using the code passing the title line with some space ("E xample") or with the tab (\ t) or using the following code:

let attributedString = NSMutableAttributedString(string: "Example") attributedString.addAttribute(NSKernAttributeName, value: CGFloat(1.4), range: NSRange(location: 0, length: 9)) 

Then you assign the attributed string to your header.

+3
source

bellow function works for me, hope this helps too.

 func setCharacterSpacig(string:String) -> NSMutableAttributedString { let attributedStr = NSMutableAttributedString(string: string) attributedStr.addAttribute(NSKernAttributeName, value: 1.25, range: NSMakeRange(0, attributedStr.length)) return attributedStr } 
+1
source

Cool extension for UILabel:

 extension UILabel{ func setCharacterSpacing(_ spacing: CGFloat){ let attributedStr = NSMutableAttributedString(string: self.text ?? "") attributedStr.addAttribute(NSAttributedString.Key.kern, value: spacing, range: NSMakeRange(0, attributedStr.length)) self.attributedText = attributedStr } } 

Using:

 label.setCharacterSpacing(4) 
0
source

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


All Articles