UITextField使用方法

最近使用UITextField 和 UITextView 。对于其相关内容进行了一些总结,主要方便自己以后的使用

UITextField :

只能输入一行,不可以滚动显示浏览全文,可以设置提醒文字(有placeholder属性),有占位,继承自UIView[UIControl]。

UITextField的常规设置

//初始化textfield并设置位置及大小
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)];

//设置边框样式,只有设置了才会显示边框样式
textField.borderStyle = UITextBorderStyleRoundedRect;
  typedef NS_ENUM(NSInteger, UITextBorderStyle) {
    UITextBorderStyleNone,  // 默认样式 无边框
    UITextBorderStyleLine,   // 边框加线条
    UITextBorderStyleBezel, // 边框加灰色线条  
    UITextBorderStyleRoundedRect // 圆角 最常用
};

//设置输入框的背景颜色, 若是使用了自定义的背景图片边框会被忽略掉
textField.backgroundColor = [UIColor whiteColor];

//设置背景
textField.background = [UIImage imageNamed:@"aaaaa.png"];

//设置背景
textField.disabledBackground = [UIImage imageNamed:@"bbbb.png"];

//当输入框没有内容时,占位字符 提示内容为password
textField.placeholder = @"password";

//修改placeholder的颜色
[textField setValue:UIColorFromRGB(0xCCCCCC) forKeyPath:@"_placeholderLabel.textColor"];

// 设置字体样式和大小
textField.font = [UIFont fontWithName:@"Arial" size:20.0f];

// 设置系统默认字体大小
textField.font = [UIFont systemFontOfSize:15];

//设置字体颜色
textField.textColor = [UIColor redColor];

//输入框中最右侧的删除Button的显示时机
textField.clearButtonMode = UITextFieldViewModeAlways;
typedef enum {
    UITextFieldViewModeNever, 重不出现
    UITextFieldViewModeWhileEditing, 编辑时出现
    UITextFieldViewModeUnlessEditing, 除了编辑外都出现
    UITextFieldViewModeAlways  一直出现
} UITextFieldViewMode;

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

//是否使用语密码输入
 textField.secureTextEntry = YES;

//是否纠错
textField.autocorrectionType = UITextAutocorrectionTypeNo;
typedef enum {
    UITextAutocorrectionTypeDefault, 默认
    UITextAutocorrectionTypeNo,  不自动纠错
    UITextAutocorrectionTypeYes, 自动纠错
} UITextAutocorrectionType;

//再次编辑时清空输入框的内容
  textField.clearsOnBeginEditing = YES;

//内容的垂直和水平对齐方式 UITextField继承自UIControl,此类中的属性
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
textField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

// 文字的水平对齐方式
textField.textAlignment = NSTextAlignmentCenter;
typedef NS_ENUM(NSInteger, UIControlContentVerticalAlignment) {
    UIControlContentVerticalAlignmentCenter  = 0,
    UIControlContentVerticalAlignmentTop     = 1,
    UIControlContentVerticalAlignmentBottom  = 2,
    UIControlContentVerticalAlignmentFill    = 3,
};

typedef NS_ENUM(NSInteger, UIControlContentHorizontalAlignment) {
    UIControlContentHorizontalAlignmentCenter = 0,
    UIControlContentHorizontalAlignmentLeft   = 1,
    UIControlContentHorizontalAlignmentRight  = 2,
    UIControlContentHorizontalAlignmentFill   = 3,
};

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

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



//设置键盘的样式
textField.keyboardType = UIKeyboardTypeNumberPad;

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

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

typedef enum {
    UITextAutocapitalizationTypeNone, 不自动大写
    UITextAutocapitalizationTypeWords, 单词首字母大写
    UITextAutocapitalizationTypeSentences, 句子的首字母大写
    UITextAutocapitalizationTypeAllCharacters, 所有字母都大写
} UITextAutocapitalizationType;

//return键变成什么键
textField.returnKeyType = UIReturnKeyDone;

typedef enum {
    UIReturnKeyDefault,          //默认 灰色按钮,标有Return
    UIReturnKeyGo,              //标有Go的蓝色按钮
    UIReturnKeyGoogle,          //标有Google的蓝色按钮,用语搜索
    UIReturnKeyJoin,            //标有Join的蓝色按钮
    UIReturnKeyNext,            //标有Next的蓝色按钮
    UIReturnKeyRoute,           //标有Route的蓝色按钮
    UIReturnKeySearch,          //标有Search的蓝色按钮
    UIReturnKeySend,            //标有Send的蓝色按钮
    UIReturnKeyYahoo,           //标有Yahoo的蓝色按钮
    UIReturnKeyDone,            //标有Done的蓝色按钮
    UIReturnKeyEmergencyCall,   //紧急呼叫按钮
} UIReturnKeyType;

//键盘外观
textField.keyboardAppearance=UIKeyboardAppearanceDefault;
typedef NS_ENUM(NSInteger, UIKeyboardAppearance) {
    UIKeyboardAppearanceDefault,          // 默认亮色
    UIKeyboardAppearanceDark NS_ENUM_AVAILABLE_IOS(7_0),
    UIKeyboardAppearanceLight NS_ENUM_AVAILABLE_IOS(7_0),
    UIKeyboardAppearanceAlert = UIKeyboardAppearanceDark,  // 石墨色,暗色
};


//最右侧加图片是以下代码  左侧类似
UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];
textField.rightView=image;
textField.rightViewMode = UITextFieldViewModeAlways;

typedef enum {
    UITextFieldViewModeNever,
    UITextFieldViewModeWhileEditing,
    UITextFieldViewModeUnlessEditing,
    UITextFieldViewModeAlways
} UITextFieldViewMode;

UITextField代理方法以及代理调用顺序

标有序号的是在常规使用中会调用的delegate

#pragma  mark - UITextField delegate
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    //返回一个BOOL值,指定是否循序文本字段开始编辑
    NSLog(@"1");
    return YES;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    // 准备开始输入,文本字段将成为第一响应者
    NSLog(@"2");
}

// return NO to not change text
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //输入文字时 一直监听
    //每有文字键入就会调用此处
    //也可以用来限制输入字符的长度
    if(range > 200) {
        return NO;
    }
    NSLog(@"3");
    return YES;
}

// return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    //返回BOOL值,指定是否允许文本字段结束编辑,当编辑结束,文本字段会让出第一响应者
    NSLog(@"4");
    return YES;
}

// may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    //文本彻底结束编辑时调用
    NSLog(@"5");
}



-(BOOL)textFieldShouldClear:(UITextField *)textField
{
    // 点击输入框‘x’号清除按钮时调用
    NSLog(@"6");
    return YES;
}

// 一般用来隐藏键盘
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    // 点击键盘的Retnrn会调用
    NSLog(@"7");
    [textField resignFirstResponder];//收起键盘
    return YES;
}


// if implemented, called in place of textFieldDidEndEditing:
- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason NS_AVAILABLE_IOS(10_0)
{
    
}

typedef NS_ENUM(NSInteger, UITextFieldDidEndEditingReason) {
    UITextFieldDidEndEditingReasonCommitted,
    UITextFieldDidEndEditingReasonCancelled UIKIT_AVAILABLE_TVOS_ONLY(10_0)
} NS_ENUM_AVAILABLE_IOS(10_0);

本文参考了一些其他文章,再次表示感谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值