Swift2.0 UITextView 和 UITextFile 的使用

时间:2023-07-21 19:42:08

在Swift2.0中,UITextFile 和 UITextView 的使用总体上和在OC中是一样的,今天只是给大家一段代码,然后说UITextView里面的光标位置的问题。先看他们使用的简单的代码

    func creatUIciew(){

         //  textView
let textview:UITextView = UITextView(frame: CGRectMake(10, 100, 200, 100))
textview.layer.borderWidth = 1
textview.layer.borderColor = UIColor.purpleColor().CGColor
textview.textColor=UIColor.redColor()
textview.font = UIFont .systemFontOfSize(20)
self.view .addSubview(textview) // 自动适应 ScrollerView 的插入点。
self.automaticallyAdjustsScrollViewInsets = false; // textFile
let textfile:UITextField = UITextField(frame: CGRectMake(10, 300, 200, 50))
textfile.layer.borderWidth = 1
textfile.layer.borderColor = UIColor.purpleColor().CGColor
self.view .addSubview(textfile) } // 触摸手势,回收键盘。
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { self.view .endEditing(true) }

主要说一下这个   self.automaticallyAdjustsScrollViewInsets = false 后面这个属性 automaticallyAdjustsScrollViewInsets ,看简单的字面意思其实也就够了,自动适应scrollview 的插入点。这里如果你设置成 true 的话,它的插入点你就交给了 UInavigationController 去控制了,经常会出现这样那样的奇奇怪怪的问题。但你设置成  false 的话,它就交给你控制,插入点也就按你初始化的位置插入!UITextView 其实也是继承与UIScrollView 的,所以他就能控UITextView 的光标输入位置。