首页 > 学院 > 开发设计 > 正文

UITextField的常用属性,Delegate,重绘

2019-11-14 19:54:57
字体:
来源:转载
供稿:网友

 

 

一  属性

    UITextField * myTextField = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 200, 50)];    myTextField.backgroundColor = [UIColor clearColor];    //简单设置textField的边缘样式    myTextField.borderStyle = UITextBorderStyleRoundedRect;    //占位显示的内容(水印)    myTextField.placeholder = @"请输入姓名";    //对齐模式    myTextField.textAlignment = NSTextAlignmentLeft;    //显示清除按钮的模式    myTextField.clearButtonMode = UITextFieldViewModeWhileEditing;//    设置背景图//    myTextField.disabledBackground//    每次输入字符变成点.常用于密码输入//    myTextField.secureTextEntry = YES;    //再次编辑就清空//    text.clearsOnBeginEditing = YES;    //设置键盘的样式//    text.keyboardType = UIKeyboardTypeNumberPad;    //首字母是否大写//    text.autocapitalizationType = UITextAutocapitalizationTypeNone;    //return键变成什么键//    text.returnKeyType =UIReturnKeyDone;    //键盘外观//    textView.keyboardAppearance=UIKeyboardAppearanceDefault;    //按return键键盘往下收//    becomeFirstResponder                //    UIView* view1 = [[UIView alloc]initWithFrame:CGRectMake(0,0,40,0)];//    UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"music.png"]];//    imageView.frame = CGRectMake(0,0,40,40);//    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];//    imageView.image = [UIImage imageNamed:@"music.png"];                            //左视图,视图可以是UIView下的任意视图.//    myTextField.leftView = imageView;    //左视图的显示模式//    myTextField.leftViewMode = UITextFieldViewModeAlways;    //右视图//    myTextField.rightView    //右视图显示模式//    myTextField.rightViewMode

 

二 代理方法

 

遵守TextField代理后.myTextField.delegate = self;

才能使用它的代理方法;在此就不多做说明.

 

三 TextField的重绘

除了UITextField对象的风格选项,你还可以定制化UITextField对象,为他添加许多不同的重写方法,来改变文本字段的显示行为。这些方法都会返回一个CGRect结构,制定了文本字段每个部件的边界范围。以下方法都可以重写。(需要创建一个类,继承自UITextField.来重写这些方法).
 
– textRectForBounds:     //重写来重置文字区域
– drawTextInRect:         //改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了.
– placeholderRectForBounds:  //重写来重置占位符区域
– drawPlaceholderInRect:  //重写改变绘制占位符属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了.
– borderRectForBounds:  //重写来重置边缘区域
– editingRectForBounds:  //重写来重置编辑区域
– clearButtonRectForBounds:  //重写来重置clearButton位置,改变size可能导致button的图片失真
– leftViewRectForBounds:
– rightViewRectForBounds:

 

在实际开发中,通常重写leftViewRectForBounds方法.并设置leftView,leftViewMode属性,来实现避免输入的内容顶格显示的问题.

 

 

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表