调整UITextView的行距 [英] Adjusting the line spacing of UITextView

查看:58
本文介绍了调整UITextView的行距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何调整UITextView的行高(行距)?我在哪里更改/添加代码?我目前正在尝试将代码添加到AppDelegate.swift文件中的 didFinishLaunchingWithOptions 函数中.这是我的代码:

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
}

然后,我选择UITextView并添加以下用户定义的运行时属性:

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?

推荐答案

您只是在创建一个空的段落样式.只有应用程序代表知道这一点.

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

相反,您应该在管理 UITextView 的类中为文本设置样式(没有 UITextLabel 这样的东西).获得对文本视图的引用后,可以执行以下操作:

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)

您还可以仅将样式应用于文本的指定部分( NSRange ).但这稍微复杂一点,所以您应该在这里问另一个问题.

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.

这篇关于调整UITextView的行距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆