文本控件(UITextField)继承了UIControl控件,所以作为活动控件使用!创建该控件有两种方法:一、拖控件。二、纯代码搞定。其实本质一样。不过还是建议采用纯代码,这样对控件的可定制性非常高。
Textfield=[[uitextfield Alloc]initwithframe:cgrectmake (120.0f, 80.0f, 150.0f, 30.0f)];
[TextField setborderstyle:uitextborderstyleroundedrect];//outer box type
[Email protected] "Please enter:";//words displayed by default
textfield.securetextentry=yes;//is a password-typed input box
textfield.clearbuttonmode=uitextfieldviewmodewhileediting;
textfield.delegate=self;
textfield.keyboardtype=uikeyboardtypedefault;//Keyboard Display Type
textfield.autocapitalizationtype=uitextautocorrectiontypeyes;//whether to turn on automatic alert correction function
textfield.contentverticalalignment=uicontrolcontentverticalalignmentcenter;//Center Display
typedef enum {
Uicontrolcontentverticalalignmentcenter = 0,
Uicontrolcontentverticalalignmenttop = 1,
Uicontrolcontentverticalalignmentbottom = 2,
Uicontrolcontentverticalalignmentfill = 3,
} uicontrolcontentverticalalignment;
Textfield.textcolor=[uicolor redcolor];//Font Color
Textfield.font=[uifont fontwithname:@ "Times New Roman" size:35];//font type and size
Textfield.background=[uiimage imagenamed:@ "background.png"];//background image
[self.view addSubview:textField];
Delegate method
-(void) textfielddidbeginediting: (Uitextfield *) TextField
{//The method is called when the text box is clicked
NSLog (@ "You start clicking on TextField! ”);
}
-(void) textfielddidendediting: (Uitextfield *) TextField
{//The method is called when the click is over
NSLog (@ "You end up clicking TextField! ”);}
-(BOOL) Textfieldshouldreturn: (Uitextfield *) TextField
{//Click Return to close the keyboard
A common way to turn off the keyboard is to refer to another blog post
http://blog.csdn.net/it_ds/article/details/44830695
[TextField Resignfirstresponder];
return YES;
}
Limit the number of words entered in a text box
-(BOOL) TextField: (Uitextfield ) TextField shouldchangecharactersinrange: (nsrange) range replacementstring: ( NSString ) string
{
if (range.location>=max_length) {
return NO;
}return YES;}
Summary of common methods for Uitextfield iOS base controls