iOS开发之TextView常用属性
<blockquote><p>本文介绍iOS开发的TextView控件, swift代码形式.</p>
</blockquote>
<h3>基本属性:</h3>
<pre><code>//textView尺寸和位置
let textViewWidth: CGFloat = 223
let textViewHeight: CGFloat = 198
let textViewTopView: CGFloat = 240
let textViewFrame = CGRect(x: 22, y: textViewTopView, width: textViewWidth, height: textViewHeight)
<pre><code> let textView = UITextView(frame: textViewFrame)
textView.text =" 我们的生活几乎离不号码,如电话号码、手机号码、车牌号码、门牌号码等等。号码为什么会影响一个人的运势?其实号码也包含有一定的吉凶数理,这就像姓名会影响运势命运的意义是一样的。"
</code></pre>
</code><p><code></code></p></pre><p></p>
<p>效果:<br>
<img src="https://img2018.cnblogs.com/other/753752/201908/753752-20190831164239389-261485120.png"></p>
<hr>
<h3>加上几个常用属性</h3>
<pre><code> //textView是否可编辑,是否可选中
textView.allowsEditingTextAttributes = false
textView.isSelectable = true
<pre><code> //字体颜色,背景颜色,字体大小,字体对齐方式
textView.textColor = UIColor.green
textView.backgroundColor = UIColor.orange
textView.font = UIFont.systemFont(ofSize: 12.0)
textView.textAlignment = .center
</code></pre>
</code><p><code></code></p></pre><p></p>
<p>效果:<br>
<img src="https://img2018.cnblogs.com/other/753752/201908/753752-20190831164251345-2017915019.png"></p>
<p>---~~~~</p>
<h3>改为富文本</h3>
<pre><code>// 创建可变属性字符串
let attribute = NSMutableAttributedString(string: textView.text)
// 设置段落样式
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
paragraphStyle.lineSpacing = 10
// 设置属性字典
let dic = [NSAttributedString.Key.foregroundColor: UIColor.red,
NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 22.0),
NSAttributedString.Key.paragraphStyle: paragraphStyle]
// 添加属性字典
attribute.addAttributes(dic, range: NSMakeRange(16, textView.text.count - 16))
// 设置textView的attributedText
textView.attributedText = attribute
</code></pre>
<p>效果:<br>
<img src="https://img2018.cnblogs.com/other/753752/201908/753752-20190831164254374-1951600016.png"></p>
<hr>
<h3>设置文字与边框间距,显示边框</h3>
<pre><code>//设置文本内容与边框的间距
textView.textContainerInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
<pre><code> //显示边框
textView.layer.borderColor = UIColor.cyan.cgColor
textView.layer.borderWidth = 2.0
</code></pre>
</code><p><code></code></p></pre><p></p>
<p>效果:<br>
<img src="https://img2018.cnblogs.com/other/753752/201908/753752-20190831164253989-339515259.png"></p>
<hr>
<h3>完整代码:</h3>
<pre><code>//: A UIKit based Playground for presenting user interface
<p>import UIKit<br>
import PlaygroundSupport</p>
<p>class MyViewController : UIViewController ,UITextViewDelegate{<br>
override func loadView() {<br>
let view = UIView()<br>
view.backgroundColor = .white</p>
<pre><code> self.view = view
//textView尺寸和位置
let textViewWidth: CGFloat = 223
let textViewHeight: CGFloat = 198
let textViewTopView: CGFloat = 240
let textViewFrame = CGRect(x: 22, y: textViewTopView, width: textViewWidth, height: textViewHeight)
let textView = UITextView(frame: textViewFrame)
textView.text =" 我们的生活几乎离不号码,如电话号码、手机号码、车牌号码、门牌号码等等。号码为什么会影响一个人的运势?其实号码也包含有一定的吉凶数理,这就像姓名会影响运势命运的意义是一样的。"
//textView是否可编辑,是否可选中
textView.allowsEditingTextAttributes = false
textView.isSelectable = true
//字体颜色,背景颜色,字体大小,字体对齐方式
textView.textColor = UIColor.green
textView.backgroundColor = UIColor.orange
textView.font = UIFont.systemFont(ofSize: 12.0)
textView.textAlignment = .center
// 创建可变属性字符串
let attribute = NSMutableAttributedString(string: textView.text)
// 设置段落样式
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
paragraphStyle.lineSpacing = 10
// 设置属性字典
let dic = [NSAttributedString.Key.foregroundColor: UIColor.red,
NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 22.0),
NSAttributedString.Key.paragraphStyle: paragraphStyle]
// 添加属性字典
attribute.addAttributes(dic, range: NSMakeRange(16, textView.text.count - 16))
// 设置textView的attributedText
textView.attributedText = attribute
//设置文本内容与边框的间距
textView.textContainerInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
//显示边框
textView.layer.borderColor = UIColor.cyan.cgColor
textView.layer.borderWidth = 2.0
textView.delegate = self//将ViewController当前对象赋值给TextView控件的delegate委托属性
view.addSubview(textView)
}
</code></pre>
<p>}<br>
// Present the view controller in the Live View window<br>
PlaygroundPage.current.liveView = MyViewController()</p>
</code><p><code></code></p></pre><p></p><br><br>
来源:https://www.cnblogs.com/nolang/p/11439506.html
頁:
[1]