Use of Swift-text input box (Uitextfield)

Source: Internet
Author: User

1, the creation of the text box, there are several styles: uitextborderstyle.none: no Border uitextborderstyle.line: line Border uitextborderstyle.roundedrect: Rounded rectangle border Uitextborderstyle.bezel: Edge + Shadow
1234 vartextField = UITextField(frame: CGRectMake(10,160,200,30))//设置边框样式为圆角矩形textField.borderStyle = UITextBorderStyle.RoundedRectself.view.addSubview(textField)

2, text box prompt text
1 textField.placeholder="请输入用户名"

3, the text size exceeds the text box length automatically reduces the font size, instead of hiding the ellipsis display
12 textField.adjustsFontSizeToFitWidth=true//当文字超出文本框宽度时,自动调整文字大小textField.minimumFontSize=14  //最小可缩小的字号

4, Horizontal/Vertical alignment
123456789 /** 水平对齐 **/textField.textAlignment = .Right //水平右对齐textField.textAlignment = .Center //水平居中对齐textField.textAlignment = .Left //水平左对齐 /** 垂直对齐 **/textField.contentVerticalAlignment = .Top//垂直向上对齐textField.contentVerticalAlignment = .Center//垂直居中对齐textField.contentVerticalAlignment = .Bottom//垂直向下对齐

5, Background image settings
1 textField.background=UIImage(named:"background1");

6, clear the button (the right fork in the input box)
123 textField.clearButtonMode=UITextFieldViewMode.WhileEditing//编辑时出现清除按钮 textField.clearButtonMode=UITextFieldViewMode.UnlessEditing //编辑时不出现,编辑后才出现清除按钮textField.clearButtonMode=UITextFieldViewMode.Always//一直显示清除按钮

7, setting the keyboard type associated with the text box
Default: The system defaults to the virtual keyboard
ascii capable: virtual keyboard showing English alphabet
url: Display a virtual keyboard for easy-to-enter numbers
number pad: Displays a virtual keyboard that is easy to enter a number
phone Pad: Display a virtual keyboard that facilitates dial-up calls
name Phone Pad: Display a virtual keyboard for easy chat dialing
email Address: Displays a virtual keyboard that is easy to enter email
decimal Pad: Displays a virtual keyboard for entering numbers and decimals
twitter: Display the virtual keyboard for easy Twitter
web Search: Display a virtual keyboard that is easy to write on a Web page
1 textfield.keyboardtype = uikeyboardtype numberpad
< Span style= "color: #0000ff;" >
8 so that the text box gets focus when the interface is open and pop-up input keyboard
1 textField.becomeFirstResponder()

< Span style= "color: #0000ff;" >9, causes the text box to lose focus and retract the keyboard
1 textField.resignfirstresponder()

10. Set the style of the keyboard return key
123456 textField.returnKeyType = UIReturnKeyType.Done //表示完成输入textField.returnKeyType = UIReturnKeyType.Go //表示完成输入,同时会跳到另一页textField.returnKeyType = UIReturnKeyType.Search //表示搜索textField.returnKeyType = UIReturnKeyType.Join //表示注册用户或添加数据textField.returnKeyType = UIReturnKeyType.Next //表示继续下一步textField.returnKeyType = UIReturnKeyType.Send //表示发送

11, the response of the keyboard return key
12345678910111213141516171819202122 class ViewController: UIViewController,UITextFieldDelegate {    override func viewDidLoad() {        super.viewDidLoad()        var textField = UITextField(frame: CGRectMake(10,160,200,30))        //设置边框样式为圆角矩形        textField.borderStyle = UITextBorderStyle.RoundedRect        textField.returnKeyType = UIReturnKeyType.Done        textField.delegate=self        self.view.addSubview(textField)     }        func textFieldShouldReturn(textField:UITextField) -> Bool    {        //收起键盘        textField.resignFirstResponder()        //打印出文本框中的值        println(textField.text)        return true;    }}

Use of Swift-text input box (Uitextfield)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.