简体   繁体   English

调整UITextView的行间距

[英]Adjusting the line spacing of UITextView

How do I adjust the line height (line spacing) of a UITextView? 如何调整UITextView的行高(行间距)? Where do I change / add the code? 我在哪里更改/添加代码? I'm currently trying to add code to the didFinishLaunchingWithOptions function in the AppDelegate.swift file. 我目前正在尝试将代码添加到AppDelegate.swift文件中的didFinishLaunchingWithOptions函数。 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: 然后我选择UITextView并添加以下的用户定义的运行时属性:

paragraphStyle.lineSpacing = 40

I already know I'm doing this completely wrong; 我已经知道我这样做完全错了; how I could do it right? 我怎么能做对的?

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 ). 相反,您应该在管理UITextView的类中设置文本样式(没有 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. 您还可以仅将样式应用于文本的指定部分( NSRange )。 But this is slightly more complicated, so you should ask another question here. 但这稍微复杂一些,所以你应该在这里提出另一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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