Swift - 设置UILabel、UITextView的文字行间距

有时我们需要调整  label  或  textView  的文本行间距大小,但这两个组件都没有相关属性可以直接设置。这个就需要借助富文本( NSAttributedString )来实现。

一、设置UILabel的行间距

1,效果图

左侧是默认的行间距,右侧是将行间距修改成  20
       原文:Swift - 设置UILabel、UITextView的文字行间距       原文:Swift - 设置UILabel、UITextView的文字行间距

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
原文:Swift - 设置UILabel、UITextView的文字行间距

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 方法里。
原文:Swift - 设置UILabel、UITextView的文字行间距
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值