加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift 开发: 文本输入框(UITextField)的用法

发布时间:2020-12-14 06:21:59 所属栏目:百科 来源:网络整理
导读:Swift - 文本输入框(UITextField)的用法 原文地址:http://hangge.com/blog/cache/detail_530.html 作者:yuhang 1,文本框的创建,有如下几个样式: UITextBorderStyle.None :无边框 UITextBorderStyle.Line :直线边框 UITextBorderStyle.RoundedRect

Swift - 文本输入框(UITextField)的用法

原文地址:http://hangge.com/blog/cache/detail_530.html 作者:yuhang

1,文本框的创建,有如下几个样式:
UITextBorderStyle.None:无边框
UITextBorderStyle.Line:直线边框
UITextBorderStyle.RoundedRect:圆角矩形边框
UITextBorderStyle.Bezel:边线+阴影
1
2
3
4
var textField = UITextField (frame: CGRectMake (10,160,200,30))
//设置边框样式为圆角矩形
textField.borderStyle = UITextBorderStyle . RoundedRect
self .view.addSubview(textField)

2,文本框提示文字
1
textField.placeholder= "请输入用户名"

3,文字大小超过文本框长度时自动缩小字号,而不是隐藏显示省略号
2
textField.adjustsFontSizeToFitWidth= true //当文字超出文本框宽度时,自动调整文字大小
textField.minimumFontSize=14 //最小可缩小的字号
4,水平/垂直对齐方式
4
5
6
7
8
9
/** 水平对齐 **/
textField.textAlignment = . Right //水平右对齐
Center //水平居中对齐
Left //水平左对齐
/** 垂直对齐 **/
textField.contentVerticalAlignment = . Top //垂直向上对齐
//垂直居中对齐
Bottom //垂直向下对齐
5,背景图片设置
textField.background=UIImage(named:"background1");
6,清除按钮(输入框内右侧小叉)
3
textField.clearButtonMode= UITextFieldViewMode WhileEditing //编辑时出现清除按钮
textField.clearButtonMode= UITextFieldViewMode UnlessEditing //编辑时不出现,编辑后才出现清除按钮
Always //一直显示清除按钮
7,使文本框在界面打开时就获取焦点,并弹出输入键盘
textField.becomeFirstResponder()
8,设置键盘return键的样式
6
textField.returnKeyType = UIReturnKeyType Done //表示完成输入
Go //表示完成输入,同时会跳到另一页
Search //表示搜索
textField.returnKeyType = UIReturnKeyType Join //表示注册用户或添加数据
Next //表示继续下一步
Send //表示发送
9,键盘return键的响应
1
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class ViewController : UIViewController , UITextFieldDelegate {
override func viewDidLoad() {
super .viewDidLoad()
textField = UITextField //设置边框样式为圆角矩形
RoundedRect
Done
textField.delegate= self
.view.addSubview(textField)
}
textFieldShouldReturn(textField: ) -> Bool
{
//收起键盘
textField.resignFirstResponder()
//打印出文本框中的值
println (textField.text)
return true ;
}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读