简体   繁体   中英

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).

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

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

Here is @matt 's answer in Obj. C:

  NSMutableAttributedString *mas = [self.lab.attributedText mutableCopy];
  [mas replaceCharactersInRange:NSMakeRange(0, [mas.string length]) 
    withString:@"Little poltergeists make up the principle form of material manifestation"]; 
  self.lab.attributedText = mas;

Hope this helps someone!

Swift 3 Just copy & execute this code to see result

let label = UILabel()
let stringValue = "UILabel\nLine\nSpacing"
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(NSParagraphStyleAttributeName, value: style, range: NSRange(location: 0, length: stringValue.characters.count))
label.attributedText = attrString

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM