Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILabel Line Spacing

I am trying to set the line spacing in a UILabel as specified by Mike Slutsky here. It works correctly for the text I specify from the Storyboard. When I try to set the UILabel.text in code, it reverts back to the default line spacing. Can someone help me understand how to either:

  1. Keep it from reverting to default, and use the settings I specified on the Storyboard or

  2. Set the value in code.

I've seen a lot of examples around using NSAttribute and NSParagraph, but since it's now possible to set in the Storyboard, I would expect their may be a more straightforward answer. Many thanks for the help!

I set the "Height Multiple" as illustrated in the above link, and my only code is as follows:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var textView: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        textView.text = "Some example text"
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

If I remove the textView.text line, it displays correctly, otherwise it's set back to the default line spacing (or Height Multiple).

like image 784
Robert Avatar asked May 22 '15 00:05

Robert


People also ask

How do I change line spacing in UILabel?

"Short answer: you can't. To change the spacing between lines of text, you will have to subclass UILabel and roll your own drawTextInRect, or create multiple labels." This is a really old answer, and other have already addded the new and better way to handle this.. Please see the up to date answers provided below.

Is line spacing the same as line height?

Line spacing is the amount of space between lines of text within a paragraph, the property set by “line-height” in HTML code. Line spacing is expressed in HTML as a number value or factor of the font size, such as 1.5× or 150%. As an example: 1.5× line height on size 12 text is 18 (by math 12 × 1.5).

What is UILabel in Swift?

A view that displays one or more lines of informational text.


2 Answers

Here's what's going on. You set things up in the storyboard with custom line spacing. This means, even though you may not know it, that in the storyboard you have set the label's attributedText.

But if you then come along and set the label's text in code, as you are doing, you throw away the attributedText and therefore all the attributes that it had. That is why, as you rightly say, things revert to the default look of the label.

The solution is: instead of setting the label's text, set its attributedText. In particular, fetch the label's existing attributedText; assign it into an NSMutableAttributedString so you can change it; replace its string while keeping the attributes; and assign it back to the label's attributedText.

So for example (I have called my label lab - your textView is a bad choice, as a text view is whole different animal):

let text = self.lab.attributedText
let mas = NSMutableAttributedString(attributedString:text)
mas.replaceCharactersInRange(NSMakeRange(0, count(mas.string.utf16)), 
    withString: "Little poltergeists make up the principle form of material manifestation")
self.lab.attributedText = mas
like image 147
matt Avatar answered Sep 30 '22 20:09

matt


UILabel has

@property(nonatomic, copy) NSAttributedString *attributedText since iOS 6.0,

This property is nil by default. Assigning a new value to this property also replaces the value of the text property with the same string data, albeit without any formatting information. In addition, assigning a new a value updates the values in the font, textColor, and other style-related properties so that they reflect the style information starting at location 0 in the attributed string.

If you set textView.text = "Some example text" again, you will loose your attributes. You should only pick one of them and not switching between them if you are sure what you are doing

like image 29
Wingzero Avatar answered Sep 30 '22 19:09

Wingzero