How to set a space between characters and strings in UILabel

I would like to set one interval between the first and second lines, between other lines I need a different interval. In this case, the second and next lines must have a special interval between characters.

All this needs to be done in one control. How can i do this? I decided to create a separate UILabel for each line, but I think this is wrong.

+4
source share
2 answers

You cannot change the spacing between lines of text, you have to subclass UILabel and collapse your own drawTextInRect, create multiple labels, or use a different font.

But there are two custom shortcuts that let you control the line.

1) https://github.com/LemonCake/MSLabel

2) https://github.com/Tuszy/MTLabel

Hope this helps ...

In iOS6, you can do this:

 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; [paragraphStyle setLineSpacing:40]; [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])]; cell.label.attributedText = attributedString ; 
+1
source

Try this, it should work for you (with Swift 4)

 let label = UILabel() let stringValue = "How to\ncontrol\nthe\nline spacing\nin UILabel" let attrString = NSMutableAttributedString(string: stringValue) var style = NSMutableParagraphStyle() style.lineSpacing = 24 // change line spacing between paragraph like 36 or 48 style.minimumLineHeight = 20 // change line spacing between each line like 30 or 40 attrString.addAttribute(NSAttributedStringKey.paragraphStyle, value: style, range: NSRange(location: 0, length: stringValue.characters.count)) // add strike attrString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: 2, range: NSMakeRange(0, attrString.length)) // add space between characters attrString.addAttribute(NSAttributedStringKey.kern, value: 2, range: NSMakeRange(0, attrString.length)) label.attributedText = attrString 


Result:

enter image description here

0
source

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


All Articles