怎么让textfield的内容文字向右偏移

怎么让textfield的内容文字向右偏移

分类:UI控件知识

//初始化textfield并设置位置及大小

UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)];

//设置边框样式,只有设置了才会显示边框样式

text.borderStyle = UITextBorderStyleRoundedRect;

typedef enum {

UITextBorderStyleNone,

UITextBorderStyleLine,

UITextBorderStyleBezel,

UITextBorderStyleRoundedRect

} UITextBorderStyle;

//设置输入框的背景颜色,此时设置为白色 如果使用了自定义的背景图片边框会被忽略掉

text.backgroundColor = [UIColor whiteColor];

//设置背景

text.background = [UIImage imageNamed:@"dd.png"];

//设置背景

text.disabledBackground = [UIImage imageNamed:@"cc.png"];

//当输入框没有内容时,水印提示 提示内容为password

text.placeholder = @"password";

//设置输入框内容的字体样式和大小

text.font = [UIFont fontWithName:@"Arial" size:20.0f];

//设置字体颜色

text.textColor = [UIColor redColor];

//输入框中是否有个叉号,在什么时候显示,用于一次性删除输入框中的内容

text.clearButtonMode = UITextFieldViewModeAlways;

typedef enum {

UITextFieldViewModeNever,  重不出现

UITextFieldViewModeWhileEditing, 编辑时出现

UITextFieldViewModeUnlessEditing,  除了编辑外都出现

UITextFieldViewModeAlways  一直出现

} UITextFieldViewMode;

//输入框中一开始就有的文字

text.text = @"一开始就在输入框的文字";

//每输入一个字符就变成点 用语密码输入

text.secureTextEntry = YES;

//是否纠错

text.autocorrectionType = UITextAutocorrectionTypeNo;

typedef enum {

UITextAutocorrectionTypeDefault, 默认

UITextAutocorrectionTypeNo,  不自动纠错

UITextAutocorrectionTypeYes,  自动纠错

} UITextAutocorrectionType;

//再次编辑就清空

text.clearsOnBeginEditing = YES;

//内容对齐方式

text.textAlignment = UITextAlignmentLeft;

//内容的垂直对齐方式  UITextField继承自UIControl,此类中有一个属性contentVerticalAlignment

text.contentVerticalAlignment= UIControlContentVerticalAlignmentCenter;

//设置为YES时文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动

textFied.adjustsFontSizeToFitWidth = YES;

//设置自动缩小显示的最小字体大小

text.minimumFontSize = 20;

//设置键盘的样式

text.keyboardType = UIKeyboardTypeNumberPad;

typedef enum {

UIKeyboardTypeDefault,      默认键盘,支持所有字符

UIKeyboardTypeASCIICapable,  支持ASCII的默认键盘

UIKeyboardTypeNumbersAndPunctuation,  标准电话键盘,支持+*#字符

UIKeyboardTypeURL,            URL键盘,支持.com按钮 只支持URL字符

UIKeyboardTypeNumberPad,              数字键盘

UIKeyboardTypePhonePad,    电话键盘

UIKeyboardTypeNamePhonePad,  电话键盘,也支持输入人名

UIKeyboardTypeEmailAddress,  用于输入电子 邮件地址的键盘

UIKeyboardTypeDecimalPad,    数字键盘 有数字和小数点

UIKeyboardTypeTwitter,        优化的键盘,方便输入@、#字符

UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,

} UIKeyboardType;

默认情况下,当向textField输入文字时,文字会紧贴在textField左边框上.我们可以通过设置textField的leftView,设置一个只有宽度的leftView.这样还不够,因为默认leftView是不显示的.还需要将leftViewMode设置为UITextFieldViewModeAlways.这样就完成了.

(小技巧:通过查询UITextField的头文件可以看出leftView和leftViewMode这两个属性写在了一组,与其他属性之间是分开的,说明这两个属性的关系很紧密)

//设置文本框左边的viewUITextField *textField = [[UITextField alloc]init];textField.frame = CGRectMake(10,30,300,30);[self.view addSubview:textField];textField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0,0,8,0)];//设置显示模式为永远显示(默认不显示)textField.leftViewMode = UITextFieldViewModeAlways;

你可能感兴趣的:(怎么让textfield的内容文字向右偏移)