本文介紹iOS開發的TextView控件, swift代碼形式.
基本屬性:
//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
效果:
完整代碼:
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupportclass MyViewController : UIViewController ,UITextViewDelegate{
override func loadView() {
let view = UIView()
view.backgroundColor = .whiteself.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) }
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()