swift中UITextView的使用
2017-01-07 14:02
288 查看
[objc]
view plain
copy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
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()
[objc]
view plain
copy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
// 字体设置
textview.textAlignment = NSTextAlignment.Left
textview.textColor = UIColor.redColor()
textview.font = UIFont(name: "GillSans", size: 15.0)
[objc]
view plain
copy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
// 光标颜色
textview.tintColor = UIColor.greenColor()
textview.editable = true
textview.userInteractionEnabled = true
textview.scrollEnabled = true
textview.showsHorizontalScrollIndicator = true
textview.showsVerticalScrollIndicator = true
[objc]
view plain
copy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
// 代理,注意添加代理协议,及实现代理方法
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")
}
[objc]
view plain
copy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
// 输入设置
textview.keyboardType = UIKeyboardType.WebSearch
textview.returnKeyType = UIReturnKeyType.Done
[objc]
view plain
copy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
// 自定义输入源控件
// 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, 0.0, CGRectGetWidth(self.view.bounds), 40.0))
accessoryview.backgroundColor = UIColor.greenColor()
let accessoryLeft = UIButton(frame: CGRectMake(10.0, 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), 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("确定")
}
[objc]
view plain
copy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
// 其他
// 第一响应,即进入编辑状态
// 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))
}
}
}
![](http://img.blog.csdn.net/20160928180332283?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20160928180346188?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20160928180403548?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
源码:https://github.com/potato512/SYSwiftLearning
view plain
copy
![](https://code.csdn.net/assets/CODE_ico.png)
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()
[objc]
view plain
copy
![](https://code.csdn.net/assets/CODE_ico.png)
// 字体设置
textview.textAlignment = NSTextAlignment.Left
textview.textColor = UIColor.redColor()
textview.font = UIFont(name: "GillSans", size: 15.0)
[objc]
view plain
copy
![](https://code.csdn.net/assets/CODE_ico.png)
// 光标颜色
textview.tintColor = UIColor.greenColor()
textview.editable = true
textview.userInteractionEnabled = true
textview.scrollEnabled = true
textview.showsHorizontalScrollIndicator = true
textview.showsVerticalScrollIndicator = true
[objc]
view plain
copy
![](https://code.csdn.net/assets/CODE_ico.png)
// 代理,注意添加代理协议,及实现代理方法
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")
}
[objc]
view plain
copy
![](https://code.csdn.net/assets/CODE_ico.png)
// 输入设置
textview.keyboardType = UIKeyboardType.WebSearch
textview.returnKeyType = UIReturnKeyType.Done
[objc]
view plain
copy
![](https://code.csdn.net/assets/CODE_ico.png)
// 自定义输入源控件
// 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, 0.0, CGRectGetWidth(self.view.bounds), 40.0))
accessoryview.backgroundColor = UIColor.greenColor()
let accessoryLeft = UIButton(frame: CGRectMake(10.0, 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), 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("确定")
}
[objc]
view plain
copy
![](https://code.csdn.net/assets/CODE_ico.png)
// 其他
// 第一响应,即进入编辑状态
// 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://github.com/potato512/SYSwiftLearning
相关文章推荐
- swift中UITextView的使用
- Swift2.0 UITextView 和 UITextFile 的使用
- swift中UITextView的使用
- IQKeyBoardManager一句话解决键盘遮挡问题(使用uitextfiled和uitextview)
- python_swift_project_swift使用
- Swift 4.0学习之:@objc的使用
- Swift-CGRect,CGSize,CGPoint 使用
- iOS.swift 关于swift的自动布局snapkit第三方的使用记录
- swift 快速奔跑的兔几 本节的内容是:SceneKit 第三说 使用材料 法向贴图
- swift中可选值?和!使用的方法示例
- 如何在 Swift 语言下使用 iOS Charts API 制作漂亮图表?
- [Swift]不使用storyboard创建视图
- Swift String 高级使用 Index 和 Rang
- UITextView使用dataDetectorTypes属性时的问题
- Swift学习之map、flatMap、filter、reduce的使用
- swift 实践- 02 -- 自定义cell 的简单使用
- swift中UIButton的使用
- 如何使用iOS 8的虚化效果(Swift版本)
- Swift - 使用相机拍摄照片
- Swift-GCD的基本使用(Swift 3.0)