Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjusting the line spacing of UITextView

How do I adjust the line height (line spacing) of a UITextView? Where do I change / add the code? I'm currently trying to add code to the didFinishLaunchingWithOptions function in the AppDelegate.swift file. Here is my code:

func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions:     NSDictionary!) -> Bool {
    // Override point for customization after application launch.
    var paragraphStyle = NSMutableParagraphStyle()
    return true
}

Then I select the UITextView and add User Defined Runtime Attributes of the following:

paragraphStyle.lineSpacing = 40

I already know I'm doing this completely wrong; how I could do it right?

like image 582
Steven Schafer Avatar asked Aug 17 '14 11:08

Steven Schafer


1 Answers

You are simply creating an empty paragraph style. Only the app delegate knows about it.

Instead, you should style your text in the class that manages the UITextView (there is no such thing as a UITextLabel). After you have obtained a reference to your text view you can do this:

let style = NSMutableParagraphStyle()
style.lineSpacing = 40
let attributes = [NSParagraphStyleAttributeName : style]
textView.attributedText = NSAttributedString(string: yourText, attributes:attributes)

You can also apply a style only to a specified portion (NSRange) of a text. But this is slightly more complicated, so you should ask another question here.

like image 179
Mundi Avatar answered Oct 29 '22 23:10

Mundi