xcode 制作自定义文本字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31873639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:38:45  来源:igfitidea点击:

Make Custom Text Fields

iosobjective-ciphonexcodeswift

提问by Ahmed jamal

I'm Beginner in IOS development, and now I'm creating a form for user registration. However the text field that is default in xcode doesn't look pretty please anyone can tell me how to make a custom text field such us in Facebook application or Lynda application.

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

回答by Anbu.Karthik

if you want to change in Storyboardor xibusing

如果你想改变Storyboardxib使用

Attribute

属性

enter image description here

在此处输入图片说明

In UITextFieldAttribute property has BorderStyleset as None, you can customize the width and height , whatever you need you can change

UITextFieldAttribute 属性BorderStyle设置为 None,您可以自定义宽度和高度,您可以随意更改

the UITextBorderStyleProperty type as

UITextBorderStyle物业类型

UITextBorderStyleNone,
UITextBorderStyleLine,
UITextBorderStyleBezel,
UITextBorderStyleRoundedRect

If set to UITextBorderStyleRoundedRect, custom background images are ignored.else three things you can customize.

如果设置为UITextBorderStyleRoundedRect,自定义背景图像将被忽略。另外三件事你可以自定义。

additional apple Reference

额外的苹果参考

programmatically

以编程方式

Objective-C

目标-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];

Swift

迅速

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)

回答by Honey Lakhani

There are various properties in UITextField class which you can use to customize the text field. e.g borderStyle property to customize the border whether you want rounded rect or line etc.. Go through the below link : https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextField_Class/

UITextField 类中有各种属性可用于自定义文本字段。例如,borderStyle 属性可以自定义边框,无论您是想要圆角矩形还是线条等。请访问以下链接:https: //developer.apple.com/library/ios/documentation/UIKit/Reference/UITextField_Class/

回答by Lucifron

Create a subclass that inherits from UITextfield, then rewrite some methods or add some custom views in layoutsubviews. And you can check this page from apple to see what UITextfield have. https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextField_Class/

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

回答by Pepeng Hapon

If you just want to change the UI of the textField, you can do this in your storyboard or nib:

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

enter image description here

在此处输入图片说明

Notice the Border Style, the default one is RoundRect (the 4th one). but if you want to customize the UI, choose the 1st one (BorderStyle: None)

注意Border Style,默认的是RoundRect(第四个)。但如果您想自定义 UI,请选择第一个 (BorderStyle: None)

Now you can put background image or change it's color and text as you what you wanted.

现在,您可以根据需要放置背景图像或更改其颜色和文本。