制作自定义文本字段

时间:2015-08-07 08:58:37

标签: ios objective-c iphone xcode swift

我是IOS开发的初学者,现在我正在创建一个用户注册表单。但是,xcode中默认的文本字段看起来并不漂亮,请任何人都可以告诉我如何在Facebook应用程序或Lynda应用程序中创建自定义文本字段。

4 个答案:

答案 0 :(得分:8)

如果您想使用

更改Storyboardxib

<强>属性

enter image description here

UITextField属性属性BorderStyle设置为None,您可以自定义宽度和高度,无论您需要什么,都可以更改

UITextBorderStyle属性类型为

UITextBorderStyleNone,
UITextBorderStyleLine,
UITextBorderStyleBezel,
UITextBorderStyleRoundedRect

如果设置为UITextBorderStyleRoundedRect,则会忽略自定义背景图片。您可以自定义三件事。

额外apple Reference

编程

Objective-C

UITextField *txtEmailfield = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
txtEmailfield.font = [UIFont systemFontOfSize:15];
txtEmailfield.placeholder = @"Email(Required)";
txtEmailfield.autocorrectionType = UITextAutocorrectionTypeNo;
txtEmailfield.keyboardType = UIKeyboardTypeDefault;
txtEmailfield.returnKeyType = UIReturnKeyDone;
txtEmailfield.clearButtonMode = UITextFieldViewModeWhileEditing;
txtEmailfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
txtEmailfield.delegate = self;
txtEmailfield.borderStyle=UITextBorderStyleNone;
[self.view addSubview:txtEmailfield];

<强>夫特

var txtEmailfield = UITextField(frame: CGRectMake(10.0, 20.0, 300.0,40.0))
txtEmailfield.backgroundColor = UIColor.redColor()
txtEmailfield.borderStyle = UITextBorderStyle.None
txtEmailfield.font=UIFont.systemFontOfSize(12)
txtEmailfield.placeholder="Email(Required)"
txtEmailfield.autocorrectionType=UITextAutocorrectionType.No
txtEmailfield.keyboardType=UIKeyboardType.Default
txtEmailfield.returnKeyType=UIReturnKeyType.Done
txtEmailfield.delegate = self
txtEmailfield.clearButtonMode=UITextFieldViewMode.WhileEditing
txtEmailfield.contentVerticalAlignment=UIControlContentVerticalAlignment.Center
self.view.addSubview(txtEmailfield)

答案 1 :(得分:0)

UITextField类中有各种属性可用于自定义文本字段。例如borderStyle属性来自定义边框是否要圆角矩形或直线等。 点击以下链接: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextField_Class/

答案 2 :(得分:0)

创建一个继承自UITextfield的子类,然后重写一些方法或在layoutsubviews中添加一些自定义视图。你可以从苹果查看这个页面,看看UITextfield有什么。 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextField_Class/

答案 3 :(得分:0)

如果您只想更改textField的UI,可以在故事板或笔尖中执行此操作:

enter image description here

注意边框样式,默认值为RoundRect(第4个)。但如果要自定义UI,请选择第一个(BorderStyle:None)

现在您可以根据需要添加背景图像或更改颜色和文本。