How to implement UILabel line spacing using xib?

I want to align the text of UILabel vertically with line spacing over xib in iOS, for example:

  hello, how are you? 

Please help me.

+6
source share
4 answers

Yes, you can adjust the spacing between lines via xib. Click UIlabel on xib, then change the type of the text property to Attributed, and then view the following images. Then enter a new line (press ctrl-return) and hold the cursor between the two lines, then adjust the line property and the Max Height property you want. My UILael text is "Hello, how are you?"

enter image description here

+10
source

Create this simple subclass of UILabel:

 @interface CustomLabel : UILabel @property (assign, nonatomic) CGFloat myLineSpacing; @end @implementation CustomLabel - (void)setMyLineSpacing:(CGFloat)myLineSpacing { _myLineSpacing = myLineSpacing; self.text = self.text; } - (void)setText:(NSString *)text { NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineSpacing = _myLineSpacing; paragraphStyle.alignment = self.textAlignment; NSDictionary *attributes = @{NSParagraphStyleAttributeName: paragraphStyle}; NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:attributes]; self.attributedText = attributedText; } 

Set the value of the myLineSpacing property with IB.

screenshot

However, you cannot view in IB, but the line spacing is in xib !
You can set label.text in your code without worrying about line spacing.

Note:
do not enter the property name " lineSpacing ". UILabel does have an undocumented lineSpacing property, and overriding something breaks.

+6
source

* From Interface Builder: **

enter image description here

Program:

Swift 4

Using a shortcut extension

 extension UILabel { // Pass value for any one of both parameters and see result func setLineSpacing(lineSpacing: CGFloat = 0.0, lineHeightMultiple: CGFloat = 0.0) { guard let labelText = self.text else { return } let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing = lineSpacing paragraphStyle.lineHeightMultiple = lineHeightMultiple let attributedString:NSMutableAttributedString if let labelattributedText = self.attributedText { attributedString = NSMutableAttributedString(attributedString: labelattributedText) } else { attributedString = NSMutableAttributedString(string: labelText) } // Line spacing attribute attributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attributedString.length)) self.attributedText = attributedString } } 

Now call the extension function

 let label = UILabel() let stringValue = "How\nto\nimplement\nUILabel\nline\nspacing\nusing\nxib?" // Pass value for any one argument - lineSpacing or lineHeightMultiple label.setLineSpacing(lineSpacing: 2.0) . // try values 1.0 to 5.0 // or try lineHeightMultiple //label.setLineSpacing(lineHeightMultiple = 2.0) // try values 0.5 to 2.0 

Or using a label instance (just copy and execute this code to see the result)

 let label = UILabel() let stringValue = "How\nto\nimplement\nUILabel\nline\nspacing\nusing\nxib?" 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 // Line spacing attribute attrString.addAttribute(NSAttributedStringKey.paragraphStyle, value: style, range: NSRange(location: 0, length: stringValue.characters.count)) // Character spacing attribute attrString.addAttribute(NSAttributedStringKey.kern, value: 2, range: NSMakeRange(0, attrString.length)) label.attributedText = attrString 
+2
source

Enter it exactly the way you want it in any text editor such as TextEdit, etc., and then paste the text section of your shortcut into xib.

But make sure your label height matches the content more and also increases the number of lines. For this case, it may be 3 or more.

enter image description here

-7
source

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


All Articles