swift中UITextView的使用

swift中UITextView的使用,第1张

概述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(
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()
@H_502_2@// 字体设置textvIEw.textAlignment = NSTextAlignment.lefttextvIEw.textcolor = UIcolor.redcolor()textvIEw.Font = UIFont(name: "GillSans",size: 15.0)
// 光标颜色textvIEw.tintcolor = UIcolor.greencolor()        textvIEw.editable = truetextvIEw.userInteractionEnabled = truetextvIEw.scrollEnabled = truetextvIEw.showsHorizontalScrollindicator = truetextvIEw.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.WebSearchtextvIEw.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: - clickfunc click(button:UIbutton){        self.vIEw.endEditing(true)}    // MARK: - left/right clickfunc 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))            }        }}


总结

以上是内存溢出为你收集整理的swift中UITextView的使用全部内容,希望文章能够帮你解决swift中UITextView的使用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1071772.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存