iOS控件之UITextField

文本输入控件UITextField

 一,作用:用户输入文字

 二,常用属性


// 设置和获取文本内容,默认nil
@property(nonatomic,copy)   NSString   *text;


// 设置文本内容颜色
@property(nonatomic,retain) UIColor  *textColor;


// 设置字体
@property(nonatomic,retain) UIFont   *font


// 对齐样式
@property(nonatomic)  NSTextAlignment  textAlignment;


// 设置风格,默认没有风格,需要设置
@property(nonatomic)    UITextBorderStyle   borderStyle;


// 提示用户输入内容文本
@property(nonatomic,copy)   NSString  *placeholder;


// 用户编辑时是否clear内容,默认为NO
@property(nonatomic)   BOOL   clearsOnBeginEditing;


// 自适应调整字体大小,默认为NO
@property(nonatomic)    BOOL    adjustsFontSizeToFitWidth;


三,常用属性

// 设置代理
@property(nonatomic,assign) id<UITextFieldDelegate> delegate;


// 设置背景,需要将textField实例的风格设置为None
@property(nonatomic,retain) UIImage *background;


// 设置textField不可用时的背景图片
@property(nonatomic,retain) UIImage *disabledBackground;


// 设置是否可编辑
@property(nonatomic,readonly,getter=isEditing) BOOL editing;


// 清除按钮的模式,默认不出现
@property(nonatomic)         UITextFieldViewMode  clearButtonMode;


// 自定义左视图
@property(nonatomic,retain) UIView   *leftView;


// 自定义左视图出现的模式
@property(nonatomic)         UITextFieldViewMode  leftViewMode;


// 不用系统键盘,自定义键盘
@property (readwrite, retain) UIView *inputView;


// 系统键盘和自定义键盘共存
@property (readwrite, retain) UIView *inputAccessoryView;


// 自动大写类型
@property(nonatomic) UITextAutocapitalizationType autocapitalizationType;


// 检查拼写是否正确
@property(nonatomic) UITextAutocorrectionType autocorrectionType;


// 修改键盘类型
@property(nonatomic) UIKeyboardType keyboardType;


// 修改返回类型
@property(nonatomic) UIReturnKeyType returnKeyType;


// 是否安全输入,比如用户输入密码
@property(nonatomic,getter=isSecureTextEntry) BOOL secureTextEntry;



四,UITextFieldDemo

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    self.window.backgroundColor = [UIColor purpleColor];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(90, 80, 140, 35);
    [button addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:button];
    
    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(60, 180, 200, 35)];
    tf.tag = 101;
    tf.delegate = self; // 设置代理
    tf.textColor = [UIColor redColor];
    tf.placeholder = @"用来提示用户";
    tf.adjustsFontSizeToFitWidth = YES;
    tf.clearsOnBeginEditing = YES;
    tf.clearButtonMode = UITextFieldViewModeWhileEditing;
//    tf.background = [UIImage imageNamed:@"navigation"];
    tf.borderStyle = UITextBorderStyleRoundedRect;
    [self.window addSubview:tf];
    [tf release];
    
    // 成为第一响应者
//    [tf becomeFirstResponder];
    
    // 自定义clear按钮
//    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
//    view.backgroundColor = [UIColor yellowColor];
//    tf.rightView = view;
//    [view release];
//    tf.rightViewMode = UITextFieldViewModeUnlessEditing;
    
    // 自定义系统键盘
//    UIView *csView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
//    csView.backgroundColor = [UIColor yellowColor];
//    tf.inputView = csView;
//    [csView release];
    
    // 共用
//    UIView *csView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
//    csView.backgroundColor = [UIColor yellowColor];
//    tf.inputAccessoryView = csView;
//    [csView release];
//    tf.secureTextEntry = YES;//设置密码
//    tf.keyboardType = UIKeyboardTypeNumberPad;
//    tf.returnKeyType = UIReturnKeyDone;
    tf.autocapitalizationType = UITextAutocapitalizationTypeNone;
    
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)test
{
    UITextField *tf = (UITextField *)[self.window viewWithTag:101];
    // 将键盘移除
    [tf resignFirstResponder];
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值