Swift UI - 多行文本输入框(UITextView)

注:代码已升级至Swift4

多行文本控件的创建

文本输入框支持的样式:

1
2
3
4
let textview = UITextView(frame:CGRect(x:10, y:100, width:200, height:100))
textview.layer.borderWidth = 1 //边框粗细
textview.layer.borderColor = UIColor.gray.cgColor //边框颜色
self.view.addSubview(textview)

文本内容

1
2
//设置显示内容
textView.text ="Welcome to AppBlog.CN"

是否可滚动

1
2
//是否可以滚动
textView.scrollEnabled = true

文本高度

1
2
3
4
5
//自适应高度
textView.autoresizingMask = UIViewAutoresizing.FlexibleHeight

//获取内容整体高度
var height:CGFloat = textView.contentSize.height

是否可编辑

1
textview.isEditable = false

内容是否可选

1
textview.isSelectable = false

文本选中

1
2
3
//选中一段文本
textView.becomeFirstResponder()
textView.selectedRange = NSMakeRange(30, 10)

字体及颜色

1
2
3
4
5
6
7
8
9
//背景颜色设置
textView.backgroundColor = UIColor.grayColor()

//设置textview里面的字体颜色
textView.textColor = UIColor.greenColor()

//设置文本字体
textView.font = UIFont.systemFontOfSize(18); //使用系统默认字体,指定14号字号
textView.font = UIFont(name: "Helvetica-Bold", size: 18) //指定字体,指定字号

设置对齐方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//文本对齐方式
textView.textAlignment = .left

//其中UITextField的文本的对齐方式
public enum NSTextAlignment : Int {
case left // Visually left aligned
case center // Visually centered
case right // Visually right aligned
/* !TARGET_OS_IPHONE */
// Visually right aligned
// Visually centered
case justified // Fully-justified. The last line in a paragraph is natural-aligned.
case natural // Indicates the default alignment for script
}

设置圆角

1
2
3
//文本视图设置圆角
textView.layer.cornerRadius = 20
textView.layer.masksToBounds = true

设置富文本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//设置富文本
var attributeString:NSMutableAttributedString=NSMutableAttributedString(string: "经常听到,押金不退,晚一天交房租,被讹了。网上报价不真实?经常被忽悠")

//设置字体颜色
attributeString.addAttribute(NSForegroundColorAttributeName, value: UIColor.greenColor(), range: NSMakeRange(0, attributeString.length))

//文本所有字符字体HelveticaNeue-Bold,16号
attributeString.addAttribute(NSFontAttributeName, value: UIFont(name: "HelveticaNeue-Bold", size: 16)!, range: NSMakeRange(0, attributeString.length))

//文本0开始5个字符字体HelveticaNeue-Bold,16号
attributeString.addAttribute(NSFontAttributeName, value: UIFont(name: "HelveticaNeue-Bold", size: 26)!, range: NSMakeRange(0, 5))

//设置字体颜色
attributeString.addAttribute(NSForegroundColorAttributeName, value: UIColor.whiteColor(), range: NSMakeRange(0, 3))

//设置文字背景颜色
attributeString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.orangeColor(), range: NSMakeRange(3, 3))

//赋值富文本
textView.attributedText = attributeString

给文字中的电话号码和网址自动加链接

1
2
3
4
5
6
7
textview.dataDetectorTypes = []  //都不加链接
textview.dataDetectorTypes = UIDataDetectorTypes.phoneNumber //只有电话加链接
textview.dataDetectorTypes = UIDataDetectorTypes.link //只有网址加链接
textview.dataDetectorTypes = UIDataDetectorTypes.all //电话和网址都加

//是否允许点击链接和附件
textView.isSelectable = true

返回键类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//返回键的类型
textView.returnKeyType = UIReturnKeyType.Done

//键盘的返回键类型
public enum UIReturnKeyType : Int {
case `default`
case go
case google
case join
case next
case route
case search
case send
case yahoo
case done
case emergencyCall
@available(iOS 9.0, *)
case `continue`
}

键盘类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//键盘类型
textView.keyboardType = UIKeyboardType.Default
键盘的类型也是比较多的
public enum UIKeyboardType : Int {
case `default` // Default type for the current input method.
case asciiCapable // Displays a keyboard which can enter ASCII characters
case numbersAndPunctuation // Numbers and assorted punctuation.
case URL // A type optimized for URL entry (shows . / .com prominently).
case numberPad // A number pad with locale-appropriate digits (0-9, ۰-۹, ०-९, etc.). Suitable for PIN entry.
case phonePad // A phone pad (1-9, *, 0, #, with letters under the numbers).
case namePhonePad // A type optimized for entering a person's name or phone number.
case emailAddress // A type optimized for multiple email address entry (shows space @ . prominently).
@available(iOS 4.1, *)
case decimalPad // A number pad with a decimal point.
@available(iOS 5.0, *)
case twitter // A type optimized for twitter text entry (easy access to @ #)
@available(iOS 7.0, *)
case webSearch // A default keyboard type with URL-oriented addition (shows space . prominently).
@available(iOS 10.0, *)
case asciiCapableNumberPad // A number pad (0-9) that will always be ASCII digits.
public static var alphabet: UIKeyboardType { get } // Deprecated
}

自定义选择内容后的菜单

在点选文字后会弹出菜单进行选择,复制等操作。我们可以在这个菜单上添加一些其他内容,如加上“邮件”“微信”等按钮选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

let textview = UITextView(frame:CGRect(x:10, y:100, width:200, height:100))
textview.layer.borderWidth = 1 //边框粗细
textview.layer.borderColor = UIColor.gray.cgColor //边框颜色
self.view.addSubview(textview)

let mail = UIMenuItem(title: "邮件", action: #selector(ViewController.onMail))
let weixin = UIMenuItem(title: "微信", action: #selector(ViewController.onWeiXin))
let menu = UIMenuController()
menu.menuItems = [mail,weixin]
}

@objc func onMail() {
print("mail")
}

@objc func onWeiXin() {
print("weixin")
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :