UITextView
UITextView是多行文本输入控件, 它的很多属性和UILabel是一样的,如设置字体,颜色,对齐方式.我们将学习它的其它属性和方法
1. UITextView的创建
let textView = UITextView(frame: CGRect(x: 20, y: 50, width: 300, height: 200))
textView.editable = true
textView.textColor = UIColor.redColor()
textView.font = UIFont.systemFontOfSize(24)
self.view.addSubview(textView)
运行程序, 我们发现一片空白,但是点击上面的部分会出现键盘,可以输入.为了能够看见UITextView的范围,我们给它设置边框颜色
textView.layer.borderWidth = 1
textView.layer.borderColor = UIColor.magentaColor().CGColor
运行程序,我们输入一段文字
2. UITextView自动检测数据类型
如果我们希望能给文本中的电话,网址,地址等加上链接,我们可以设置dataDetectorTypes属性,它的定义如下
@available(iOS 3.0, *)
public var dataDetectorTypes: UIDataDetectorType
我们进一步查看UIDataDetectorType的定义
public struct UIDataDetectorTypes : OptionSetType {
public init(rawValue: UInt)
public static var PhoneNumber: UIDataDetectorTypes { get } // 电话号码
public static var Link: UIDataDetectorTypes { get } // 网址
@available(iOS 4.0, *)
public static var Address: UIDataDetectorTypes { get } // 地址
@available(iOS 4.0, *)
public static var CalendarEvent: UIDataDetectorTypes { get } // 日期事件检测
public static var None: UIDataDetectorTypes { get } // 所有的都不检测
public static var All: UIDataDetectorTypes { get } // 所有的类型都检测
}
现在我们设置所有类型的数据都检测, 然后在输入的文本中添加一个网址
textView.dataDetectorTypes = .All
textView.editable = false
注意editable必须设置为false,否则不会产生链接效果
运行程序
3. UITextView的常用属性
我们查看UITextView的定义
public var text: String! // 设置文本, 同UILabel
public var font: UIFont? // 设置font, 同UILabel
public var textColor: UIColor? // 设置颜色, 同UILabel
public var textAlignment: NSTextAlignment // 设置对齐方式, , 同UILabel
@available(iOS 7.0, *)
public var selectable: Bool // 是否允许文字被选择
@available(iOS 6.0, *)
public var allowsEditingTextAttributes: Bool // 是否运行修改文本属性,如加粗/斜体/下划线,默认为false.
// 链接文本的属性设置
@available(iOS 7.0, *)
public var linkTextAttributes: [String : AnyObject]!
// 代理
weak public var delegate: UITextViewDelegate?
我们将allowsEditingTextAttributes修改为true看看效果
textView.allowsEditingTextAttributes = true
运行程序
4. UITextView的代理
我们查看UITextViewDelegate的定义
public protocol UITextViewDelegate : NSObjectProtocol, UIScrollViewDelegate {
@available(iOS 2.0, *)
optional public func textViewShouldBeginEditing(textView: UITextView) -> Bool // 将要开始编辑时调用, 若return false,则不会开始编辑
@available(iOS 2.0, *)
optional public func textViewShouldEndEditing(textView: UITextView) -> Bool // 将要结束编辑时调用, 若return false,则不会结束编辑
@available(iOS 2.0, *)
optional public func textViewDidBeginEditing(textView: UITextView) // 开始编辑时调用
@available(iOS 2.0, *)
optional public func textViewDidEndEditing(textView: UITextView) // 结束编辑时调用
@available(iOS 2.0, *)
optional public func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool // 改变为本前被调用, 如果return false, 这修改无效
@available(iOS 2.0, *)
optional public func textViewDidChange(textView: UITextView) // //文本发生改变编辑, 只要有文本输入都会调用
@available(iOS 2.0, *)
optional public func textViewDidChangeSelection(textView: UITextView) // 选择范围发生改变时调用
@available(iOS 7.0, *)
optional public func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool
@available(iOS 7.0, *)
optional public func textView(textView: UITextView, shouldInteractWithTextAttachment textAttachment: NSTextAttachment, inRange characterRange: NSRange) -> Bool
}
有一点需要注意的是:
*UITextView输入的时候return键是用于换行的, 所以如果要实现编辑完后键盘消失,我们一般在顶部添加一个完成按钮,让键盘消失*
下面我们实现一个需求, 当限制文本长度为40
textView.delegate = self
// MARK: - UITextViewDelegate
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if (textView.text as NSString).length + (text as NSString).length - range.length > 40 {
return false
}
return true
}
运行程序,我们发现文本长度到一定的时候再输入是无效的
5. 全部代码
import UIKit
class ViewController: UIViewController, UITextViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let textView = UITextView(frame: CGRect(x: 20, y: 50, width: 300, height: 200))
textView.editable = true
textView.textColor = UIColor.redColor()
textView.font = UIFont.systemFontOfSize(24)
textView.layer.borderWidth = 1
textView.layer.borderColor = UIColor.magentaColor().CGColor
textView.dataDetectorTypes = .All
textView.text = "http://www.youkuaiyun.com/"
// 这个必须有,否则不会出现链接效果
// textView.editable = false
// textView.clearsOnInsertion = true
textView.allowsEditingTextAttributes = true
textView.delegate = self
self.view.addSubview(textView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - UITextViewDelegate
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if (textView.text as NSString).length + (text as NSString).length - range.length > 40 {
return false
}
return true
}
}