微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

swift中UITextView的使用

let textview = UITextView(frame: CGRectMake(10.0,10.0,(CGRectGetWidth(self.view.bounds) - 10.0 * 2),80.0))
self.view.addSubview(textview)
        
textview.backgroundColor = UIColor.lightGrayColor()
// 字体设置
textview.textAlignment = NSTextAlignment.Left
textview.textColor = UIColor.redColor()
textview.font = UIFont(name: "GillSans",size: 15.0)

// 光标颜色
textview.tintColor = UIColor.greenColor()
        
textview.editable = true
textview.userInteractionEnabled = true
textview.scrollEnabled = true
textview.showsHorizontalScrollIndicator = true
textview.showsverticalScrollIndicator = true

// 代理,注意添加代理协议,及实现代理方法
textview.delegate = self
// 添加协议
class ViewController: UIViewController,UITextViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

   }
}

// 实现代理方法
// MARK: - UITextViewDelegate
    
func textViewShouldBeginEditing(textView: UITextView) -> Bool {
        print("1 textViewShouldBeginEditing")
        
        return true
}
    
func textViewDidBeginEditing(textView: UITextView) {
        print("2 textViewDidBeginEditing")
}
    
func textViewDidChange(textView: UITextView) {
        print("3 textViewDidChange")
}
    
func textView(textView: UITextView,shouldChangeTextInRange range: NSRange,replacementText text: String) -> Bool {
        print("4 textView")
        
        print("text:\(textView.text) length = \(textView.text?.characters.count)")
        
        // 回车时退出编辑
        if text == "\n"
        {
//            textView.endEditing(true)
            // 或
//            self.view.endEditing(true)
            // 或
            textView.resignFirstResponder()
            
            return true
        }
        return true
}
    
func textViewShouldEndEditing(textView: UITextView) -> Bool {
        print("5 textViewShouldEndEditing")
        
        return true
}
    
func textViewDidEndEditing(textView: UITextView) {
        print("6 textViewDidEndEditing")
}
// 输入设置
textview.keyboardType = UIKeyboardType.WebSearch
textview.returnKeyType = UIReturnKeyType.Done
// 自定义输入源控件
// let inputview = UIButton(frame: CGRectMake(0.0,0.0,CGRectGetWidth(self.view.bounds),100.0))
// inputview.setimage(UIImage(named: "normalImage"),forState: UIControlState.normal)
// inputview.backgroundColor = UIColor.lightGrayColor()
// inputview.addTarget(self,action: Selector("click:"),forControlEvents: UIControlEvents.TouchUpInside)
// textview.inputView = inputview
// 自定义输入源控件副视图
let accessoryview = UIView(frame: CGRectMake(0.0,40.0))
accessoryview.backgroundColor = UIColor.greenColor()
let accessoryLeft = UIButton(frame: CGRectMake(10.0,60.0,20.0))
accessoryview.addSubview(accessoryLeft)
accessoryLeft.setTitle("取消",forState: UIControlState.normal)
accessoryLeft.backgroundColor = UIColor.orangeColor()
accessoryLeft.addTarget(self,action: Selector("leftClick:"),forControlEvents: UIControlEvents.TouchUpInside)
let accessoryRight = UIButton(frame: CGRectMake((CGRectGetWidth(accessoryview.bounds) - 10.0 - 60.0),20.0))
accessoryview.addSubview(accessoryRight)
accessoryRight.setTitle("确定",forState: UIControlState.normal)
accessoryRight.backgroundColor = UIColor.orangeColor()
accessoryRight.addTarget(self,action: Selector("rightClick:"),forControlEvents: UIControlEvents.TouchUpInside)
textview.inputAccessoryView = accessoryview

// MARK: - click
func click(button:UIButton)
{
        self.view.endEditing(true)
}
    
// MARK: - left/right click
func leftClick(button:UIButton)
{
        print("取消")
}
    
func rightClick(button:UIButton)
{
        self.view.endEditing(true)
        print("确定")
}
// 其他
// 第一响应,即进入编辑状态
// textview.becomeFirstResponder()
// 结束响应,即结束编辑
// textview.resignFirstResponder()
// 发通知-输入改变
NSNotificationCenter.defaultCenter().addobserver(self,selector: Selector("textViewEditChanged:"),name: UITextViewTextDidChangeNotification,object: textview)

// MARK: - 通知响应方法
func textViewEditChanged(notification:NSNotification)
{
        let textView:UITextView! = notification.object as! UITextView
        if (textView != nil)
        {
            let text:String! = textView.text
            let length = text.characters.count
            if (length > 200)
            {
                textView.text = text.substringToIndex(text.startIndex.advancedBy(200))
            }
        }
}


原文地址:https://www.jb51.cc/swift/322924.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐