Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a space between characters and lines in UILabel

I'd like set one spacing between first and second lines, between other lines need another spacing. With this, second and next lines must have specific character spacing.

This all need doing in one control. How i can do this? I decided to create a separate UILabel for each row but i think it's wrong way.

like image 366
Multix Avatar asked Nov 02 '22 18:11

Multix


2 Answers

You can't change the spacing between lines of text, you will have to subclass UILabel and roll your own drawTextInRect, create multiple labels, or use a different font.

But there are two custom Labels, that allow you to control the lineheight.

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

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

Hope this helps...

In iOS6, you could do this:

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:40];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
cell.label.attributedText = attributedString ;
like image 53
lakshmen Avatar answered Nov 15 '22 13:11

lakshmen


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

like image 30
Krunal Avatar answered Nov 15 '22 12:11

Krunal