iOS开发:整理UITextField属性的基本用法(部分)

来源:互联网 发布:身边低调的有钱人知乎 编辑:程序博客网 时间:2024/05/01 22:26

总结了UITextView的一些基本用法之后,再来说说UITextField的基本用法,其实二者都是文本输入控件,并且都能够调用系统键盘,二者最大的区别是:UITextView支持多行输入并且可以滚动显示浏览全文,而UITextField只能单行输入;UITextView继承自UIScrollView,UITextField继承自UIView;UITextview没有placeholder属性,而UItextField有placeholder属性。可以说,在使用上我们完全可以把UITextView看作是UITextField的升级版。

在这里值得一提的是iOS中常常需要,有限制用户输入字数的要求,我们可以进行如下处理:
1、对于UITextView,我们可以在- (void)textViewDidChange:(UITextView *)textView{检测到输入变化的时候执行}和
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{超过一定字数返回NO即可},这两个方法;

2、对于UITextField,只能在 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; 方法中处理。

下面就来说说UITextField的使用方法,一些经常用到的属性。

一、基础用法:

在.h文件中声明:

@interface ProtocolViewController :UIViewController<UITextFieldDelegate>
{
UITextField *textField;
}
@property (nonatomic,retain)UItextField *textField;
@end

在.m文件中初始化:

- (void)init{
//UItextField(使用须遵守UITextFieldDelegate协议)
self.textField = [UITextField new]; 或者 self.textField = [[UITextField alloc] init]; 

self.textField.frame = CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 30); 

//设置它的委托方法
self.textField.delegate = self;

//设置是否可以编辑
self.textField.editable = YES;

//设置显示默认内容
self.textField.text = @"123456";

//设置填充占位文字
self.textField.placeholder = @"请输入密码";

//设置textField里面的字体颜色
self.textField.textColor = [UIColor blackColor];

//设置背景颜色
self.textField.backgroundColor = [UIColor whiteColor]; 

//设置背景图
self.textField.background = [UIImage imageNamed:@"11.png"];
self.textField.disabledBackground = [UIImage imageNamed:@"22.png"];

//设置文字对齐方式
self.textField.textAlignment = NSTextAlignmentLeft;

//设置字体大小
self.textField.font = [UIFont systemFontOfSize:25.0];

//设置字体名字和字体大小(综合法)
self.textField.font = [UIFont fontWithName:@"Arial"size:18.0];

// 当第二次输入的时候,清空上一次的内容
textField.clearsOnBeginEditing = YES; 

// 设置输入框的样式
self.textField.borderStyle = UITextBorderStyleRoundedRect;  

// 设置自动缩小显示的最小字体大小 
self.textField.minimumFontSize = 20.0f;   

// 设置textField的标签
self.textField.tag = 1001;   

// 设置是否以密码的圆点形式显示
self.textField.secureTextEntry = NO;   

// 设置是否启动自动提醒更新功能
self.textField.autocorrectionType = YES;   

//UITextField下的键盘中return 表示换行(让return键变成什么类型的键)
self.textField.returnKeyType = UIReturnKeyDefault; 

//设置边框属性
self.textField.layer.cornerRadius = 10; //边框弧度
self.textField.borderColor = [UIColor darkGrayColor].CGColor; //设置边框颜色
self.textField.layer.borderWidth = 2; //边框宽度

// 编辑的时候,会在最右边显示出删除全部的小x
self.textField.clearButtonMode = UITextFieldViewModeWhileEditing;  

// 输入内容的对齐方法
self.textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;  

//自适应高度
self.textField.autoresizingMask = UIViewAutoresizingFlexibleHeight; 

//设置首字母是否大写
self.textField.autocapitalizationType = UITextAutocapitalizationTypeNone;

//键盘外观
self.textField.keyboardAppearance = UIKeyboardAppearanceAlert;

//在textField最左侧加图片(在最右侧加图片同理)
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"share.png"]];
self.textField.leftView = image;
self.textField.leftViewMode = UITextFieldViewModeAlways;

//弹出的键盘样式类型
self.textField.keyboardType = UIKeyboardTypeDefault;

//键盘类型(收藏)
typedef enum {
UIKeyboardTypeDefault, 默认键盘,支持所有字符
UIKeyboardTypeASCIICapable, 支持ASCII的默认键盘
UIKeyboardTypeNumbersAndPunctuation, 标准电话键盘,支持+、*、#字符
UIKeyboardTypeURL, URL键盘,支持.com按钮 只支持URL字符
UIKeyboardTypeNumberPad, 数字键盘
UIKeyboardTypePhonePad, 电话键盘
UIKeyboardTypeNamePhonePad, 电话键盘,支持输入人名
UIKeyboardTypeEmailAddress, 用于输入电子邮件地址的键盘
UIKeyboardTypeDecimalPad, 数字键盘,有数字和小数点
UIKeyboardTypeTwitter, 优化的键盘,方便输入@、#字符
UIKeyboardTypeWebSearch,
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
} UIKeyboardType;  


[self.view addSubview: self.textField];//加入到整个页面中
}

再写一个XIB里面设置键盘的方法:


二、代理方法:

//返回一个BOOL类型的值,指定是否循序文本字段开始编辑 

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return YES;
}

//当开始点击textField会调用的方法
- (void)textFieldDidBeginEditing:(UITextField *)textField {
NSLog(@"开始编辑");
}

//当textField编辑结束时调用的方法
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSLog(@"结束编辑");
}

//指定是否允许文本字段结束编辑,当编辑结束之后文本字段会让出首个responder 
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
//这个方法对一些文本字段必须始终保持活跃状态的程序很适用,比如即时消息
return NO;
}

//用户使用自动更正功能,把输入的文字修改为推荐的文字时就会调用这个方法
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//这对于想要加入撤销选项的应用程序特别有用
//可以跟踪字段内所做的最后一次修改,也可以对所有编辑做日志记录,用作审计用途。
//这个方法的参数中有一个NSRange对象,指明了被改变文字的位置,建议修改的文本也在其中
return YES;
}

//指明是否允许根据用户请求清除内容,可以设置在特定条件下才允许清除内容 
- (BOOL)textFieldShouldClear:(UITextField *)textField{
return YES;
}

//按下Done按钮的调用方法,回收键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[self.textField resignFirstResponder];
return YES;
}

三、特殊用法

1、 修改textfield填充文字的颜色(系统默认的填充文字颜色是浅灰色,但是有些时候需要自定义填充文字颜色),例子如下
    NSMutableAttributedString* attributedString = [[NSMutableAttributedString alloc] initWithString:@"请输入提现金额"];
    NSDictionary* attributes = @{NSForegroundColorAttributeName:RGB(241,169,74)};
    [attributedString setAttributes:attributes range:NSMakeRange(0, [attributedString length])];
    [self.textField setAttributedPlaceholder:attributedString];


还有另外一种方法:修改textfield填充文字的颜色,利用Runtime访问UITextField的隐藏属性:

  第一步:/用下面这个方法来获得UITextField的所有属性
+ (void)getProperties
{
unsigned int count = 0;
objc_property_t *properties = class_copyPropertyList([UITextField class], &count);
for (int i = 0; i<count; i++) {
// 取出属性
objc_property_t property = properties[i];
//
输出属性名字
NSLog(@"%s
====%s", property_getName(property), property_getAttributes(property));
}
free(properties); //方法属于C语言,用完后
要释放内存
}

第二步:获取到成员属性:_placeholderLabel.textColor,这就是用来修改UITextField的占位文字颜色

//修改占位文字颜色
      [self setValue:self.textColor forKeyPath:@"_placeholderLabel.textColor"];


2、设置占位文字与左边有一定间距,需要重写-(void)drawPlaceholderInRect:(CGRect)rect的方法。

- (void)drawPlaceholderInRect:(CGRect)rect{
//设置距离
CGRect placeholderRect = CGRectMake(rect.origin.x + 10, (rect.size.height- self.font.pointSize)/2, rect.size.width, rect.size.height);
NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
style.lineBreakMode = NSLineBreakByTruncatingTail;
style.alignment = self.textAlignment;
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:style,NSParagraphStyleAttributeName, self.font, NSFontAttributeName,[UIColor orangeColor], NSForegroundColorAttributeName, nil];
[self.placeholder drawInRect:placeholderRect withAttributes:dic];
}


3、设置TextField的文字与左边有一定间距(设置leftView,让它总是显示)。

UIView *passView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, self.height)];
[passView setBackgroundColor:[UIColor whiteColor]];
self.leftView = passView;
self.leftViewMode = UITextFieldViewModeAlways;





1 0