有时我们需要调整
label
或
textView
的文本行间距大小,但这两个组件都没有相关属性可以直接设置。这个就需要借助富文本(
NSAttributedString
)来实现。
原文出自: www.hangge.com 转载请保留原文链接: http://www.hangge.com/blog/cache/detail_1570.html
一、设置UILabel的行间距
1,效果图
左侧是默认的行间距,右侧是将行间距修改成
20。
2,样例代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
import
UIKit
class
ViewController
:
UIViewController
{
override
func
viewDidLoad() {
super
.viewDidLoad()
let
label =
UILabel
(frame:
CGRect
(x:10, y:20, width:300, height:100))
//设置允许换行
label.numberOfLines = 0
//要显示的文字
let
str =
"欢迎访问hangge.com 欢迎访问hangge.com 欢迎访问hangge.com"
//通过富文本来设置行间距
let
paraph =
NSMutableParagraphStyle
()
//将行间距设置为28
paraph.lineSpacing = 20
//样式属性集合
let
attributes = [
NSFontAttributeName
:
UIFont
.systemFont(ofSize: 15),
NSParagraphStyleAttributeName
: paraph]
label.attributedText =
NSAttributedString
(string: str, attributes: attributes)
self
.view.addSubview(label)
}
override
func
didReceiveMemoryWarning() {
super
.didReceiveMemoryWarning()
}
}
|
二、设置UITextView的行间距
1,UITextView是只读的情况
也就是说
textview 只用来显示一段文字,不输入。那么设置行间距的方法和
label 是一样的。下面代码将行间距修改成
20。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import
UIKit
class
ViewController
:
UIViewController
{
override
func
viewDidLoad() {
super
.viewDidLoad()
let
textView =
UITextView
(frame:
CGRect
(x:10, y:20, width:300, height:100))
//要显示的文字
let
str =
"欢迎访问hangge.com 欢迎访问hangge.com 欢迎访问hangge.com"
//通过富文本来设置行间距
let
paraph =
NSMutableParagraphStyle
()
//将行间距设置为28
paraph.lineSpacing = 20
//样式属性集合
let
attributes = [
NSFontAttributeName
:
UIFont
.systemFont(ofSize: 15),
NSParagraphStyleAttributeName
: paraph]
textView.attributedText =
NSAttributedString
(string: str, attributes: attributes)
self
.view.addSubview(textView)
}
override
func
didReceiveMemoryWarning() {
super
.didReceiveMemoryWarning()
}
}
|
2,UITextView是可编辑的情况
如果是想在输入内容的时候就按照设置的行间距进行动态改变,那就需要将相关代码放到
textview 的
delegate 方法里。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
import
UIKit
class
ViewController
:
UIViewController
,
UITextViewDelegate
{
override
func
viewDidLoad() {
super
.viewDidLoad()
let
textView =
UITextView
(frame:
CGRect
(x:10, y:20, width:300, height:100))
//设置代理
textView.delegate =
self
//添加到视图中
self
.view.addSubview(textView)
}
func
textViewDidChange(_ textView:
UITextView
) {
//获取文本内容
let
str = textView.text!
//通过富文本来设置行间距
let
paraph =
NSMutableParagraphStyle
()
//将行间距设置为28
paraph.lineSpacing = 20
//样式属性集合
let
attributes = [
NSFontAttributeName
:
UIFont
.systemFont(ofSize: 15),
NSParagraphStyleAttributeName
: paraph]
textView.attributedText =
NSAttributedString
(string: str, attributes: attributes)
}
override
func
didReceiveMemoryWarning() {
super
.didReceiveMemoryWarning()
}
}
|
原文出自: www.hangge.com 转载请保留原文链接: http://www.hangge.com/blog/cache/detail_1570.html