Swift - 设置UILabel、UITextView的文字行间距
有时我们需要调整 label 或 textView 的文本行间距大小,但这两个组件都没有相关属性可以直接设置。这个就需要借助富文本( NSAttributedString )来实现。
一、设置UILabel的行间距
1,效果图
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是只读的情况
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是可编辑的情况
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
Swift - 设置UILabel、UITextView的文字行间距相关推荐
- ios label文字行间距_iOS 设置UILabel行间距【原创】
今天在项目中遇到需要设置较多文字的行间距问题,得到解决办法分享给大家. 下面贴出代码供参考: 1.正常使用 UILabel *label = [[UILabel alloc] initWithFram ...
- Swift 设置文字阴影
Swift 设置文字阴影其实很简单 我们一UILabel为例来看 let label1=UILabel(frame: CGRectMake(30, 50, 310,36))label1.text=&q ...
- html中一段文字行间距设置,CSS如何设置文字行间距?
CSS如何调节设置每行文字字体间距(行距),使得每行的文字一定的间距距离?下面本篇文章就来给大家介绍使用CSS设置文字行间距的方法,希望对大家有所帮助. 要想使得每行的文字一定的间距距离可以通过CSS ...
- PdfPCell设置单元格内文字的行间距
PdfPCell设置单元格内文字的行间距 PdfPCell cell = new PdfPCell(new Paragraph("",font); //最终行间距 = fixedL ...
- php 自定义行间距,CSS设置文字行间距的方法介绍(代码示例)
本篇文章的内容是关于CSS设置文字行间距的方法介绍,内容很详细,有需要的朋友可以参考一下. 对于行间距的设置肯定是需要行高属性line-height来设置间距.(相关推荐:CSS学习手册) 指定绝对值 ...
- php 自定义行间距,css文字行间距怎么设置?css设置行间距方法
在css样式中,很多人对css文字行间距并不是很熟悉,下面 第一PHP社区 带你了解一下css文字行间距怎么设置?以及css设置行间距方法. css文字行间距怎么设置?css设置行间距方法 在一篇文章 ...
- 有时候可用 UIWebView 代替 UITextView,解决行间距问题
转载自:http://windshg.iteye.com/blog/1807903 我们在使用 UILabel 或是 UITextView 时,如果文字出现了换行,目前我还不知道如何去控制行间距,固定 ...
- [Xcode 实际操作]四、常用控件-(5)UILabel文本标签自定义文字样式
目录:[Swift]Xcode实际操作 本文将演示给标签对象添加描边效果,在项目文件夹上,点击鼠标右键菜单, 选择[Create File]->[Cocoa Touch Class]->[ ...
- UIKit框架-高级控件Swift版本: 5.UITextView方法/属性详解
前面我们讲完了iOS中的两个表格控件, 现在让我们继续往下看: 1.UITextView常用属性 // 1.设置 UITextView 的文字 var text: String!// 2.设置 UIT ...
最新文章
- lambda表达式python_Python中的Lambda表达式
- legend2---开发日志13(layer_mobile的content传入dom 出现【object object】如何解决)
- NDK中C++标准库、STL的配置;Include其他头文件
- SharpDevelop源码分析笔记(一)
- Python OS模块使用汇总
- Java8 時間API
- 设置Proxy Server和SQL Server实现数据库安全
- 论文浅尝 | 基于知识库的自然语言理解 04#
- 【数论学习笔记】同余
- tomcat启动问题
- 打包后找不到so_RTX 3090安装cuda11.1 找不到libcusolver.so.10
- 高立数值最优化方法_最优化理论之无约束优化基本结构及其python应用
- MyBatis集合Spring(四)之使用Spring处理事务
- mysql php apache源码_用源码包安装php+mysql+apache+vbb总结
- 基于PaddleHub实现简易人像抠图
- navicat 中文注册码
- HBuilderX连接夜神模拟器
- The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals
- Python的Code对象
- 解决QQ 群共享无法打开,页面无法显示