UITextField用法大全

#import “AppDelegate.h”

@interface AppDelegate ()

@property (nonatomic, retain) UITextField *textField;
@property (nonatomic, retain) UITextField *textField2;

@end

@implementation AppDelegate

  • (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

pragma mark - 文字属性

//创建textfield并设置位置及大小

self.textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 30)];
[self.window addSubview:self.textField];

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

self.textField.text = @"";

//属性文本, 分配一个新值给这个属性, 用同样的字符串数据取代这个文本属性

self.textField.attributedText = [[NSAttributedString alloc] initWithString:@""];

//当输入框没有内容时,水印提示 (默认使用70%灰色)

self.textField.placeholder = @"WELCOME";

//当输入框没有内容时, 分配一个新值给这个属性, 用同样的字符串数据取代这个占位符属性

 //textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@""];

//默认情况下, 该属性返回值为一个文本属性的字典,

 self.textField.defaultTextAttributes = @{};

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

self.textField.font = [UIFont fontWithName:@"黑体" size:20];
//textField.font = [UIFont systemFontOfSize:20];

//设置字体颜色

self.textField.textColor = [UIColor redColor];

//设置输入框的背景颜色

self.textField.backgroundColor = [UIColor whiteColor];

//文本对齐方式

self.textField.textAlignment = 1;
/*
 enum {
    NSTextAlignmentLeft      = 0, 文本向左对齐
    NSTextAlignmentCenter    = 1, 文本居中
    NSTextAlignmentRight     = 2, 文本向右对齐
    NSTextAlignmentJustified = 3, 调整文本,以便在段落的最后一行是自然对齐。
    NSTextAlignmentNatural   = 4, 使用与当前脚本关联的默认对齐方式。
};
typedef NSInteger NSTextAlignment;
 */

//类型属性, 新的文本会被用户输入, 如果textField不是编辑模式, 属性值为空, 同样, 你不能向属性赋值除非textField是编辑模式

self.textField.typingAttributes = @{};

pragma mark - 文字大小

//设置为YES时文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动,
//如果你设置为YES, 你也需要通过minimumFontSize属性来设置最小字体大小

self.textField.adjustsFontSizeToFitWidth = YES;

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

self.textField.minimumFontSize = 10;

pragma mark - 编辑行为

//再次编辑就清空 默认为NO

self.textField.clearsOnBeginEditing = YES;

//是否插入文本取代先前内容, 默认为NO

self.textField.clearsOnInsertion = NO;

//是否允许编辑文本属性, 默认为NO

self.textField.allowsEditingTextAttributes = NO;

pragma mark - 设置视图背景

//设置边框样式

self.textField.borderStyle = UITextBorderStyleRoundedRect;
/* 
 typedef enum {
    UITextBorderStyleNone, 无边框样式, 默认为无
    UITextBorderStyleLine, 边框样式为矩形
    UITextBorderStyleBezel, 边框样式为表圈, 此样式通常被用在标准数据       输入
    UITextBorderStyleRoundedRect 边框样式为四边有弧度的圆矩形
} UITextBorderStyle;
 */

//设置背景图片,如果使用了自定义的背景图片边框会被忽略掉

self.textField.background = [UIImage imageNamed:@""];

//设置背景图片, 如果background属性没有被设置, 此属性会被忽略

self.textField.disabledBackground = [UIImage imageNamed:@""];

pragma mark - 覆盖视图

//清除文本模式,用于一次性删除输入框中的内容

self.textField.clearButtonMode = UITextFieldViewModeAlways;
/*
typedef enum {
    UITextFieldViewModeNever, 从不出现
    UITextFieldViewModeWhileEditing, 编辑时出现
    UITextFieldViewModeUnlessEditing, 除了编辑外都出现
    UITextFieldViewModeAlways  一直出现
} UITextFieldViewMode;
*/

//左视图模式, 用于在何时出现该视图, 与清除文本模式相同

self.textField.leftViewMode = UITextFieldViewModeWhileEditing;

//设置左视图, 不要忘了打开视图模式

UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 30)];
leftView.backgroundColor = [UIColor redColor];
self.textField.leftView = leftView;
[leftView release];

//右视图模式, 用于在何时出现该视图, 与清除文本模式相同

self.textField.rightViewMode = UITextFieldViewModeWhileEditing;
UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(140, 0, 10, 30)];
rightView.backgroundColor = [UIColor redColor];
self.textField.rightView = rightView;
[rightView release];

pragma mark - 绘画界面

/* 下列方法, 不可以直接调用, 需要的话可以重写此方法, 返回值都为CGRect */

//– textRectForBounds:    重写来重置文字区域

//- drawTextInRect:       改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了.

//- placeholderRectForBounds: 重写来重置占位符区域

//– drawPlaceholderInRect:  重写改变绘制占位符属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了.

//– borderRectForBounds:  重写来重置边缘区域

//– editingRectForBounds:  重写来重置编辑区域

//– clearButtonRectForBounds:  重写来重置clearButton位置,改变size可能导致button的图片失真

//– leftViewRectForBounds: 重写来重置左视图区域

//– rightViewRectForBounds: 重写来重置右视图区域

pragma mark - 代替输入

// inputView: 代替标准的系统键盘

// inputAccessoryView: 编辑时显示在系统键盘或用户自定义的inputView上面的视图

UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 20)];
inputView.backgroundColor = [UIColor redColor];
self.textField.inputAccessoryView = inputView;

//输入安全属性, 默认为NO

//textField.secureTextEntry = YES;

//自动更正功能

self.textField.autocorrectionType = UITextAutocorrectionTypeDefault;
/*
typedef NS_ENUM(NSInteger, UITextAutocorrectionType) {
    UITextAutocorrectionTypeDefault,
    UITextAutocorrectionTypeNo,
    UITextAutocorrectionTypeYes,
};
 */

pragma mark - 键盘相关 (键盘commond + K 弹出与收回)

//设置键盘的样式

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

//首字母是否大写

self.textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
/*
typedef enum {
    UITextAutocapitalizationTypeNone, 不自动大写
    UITextAutocapitalizationTypeWords, 单词首字母大写
    UITextAutocapitalizationTypeSentences, 句子的首字母大写
    UITextAutocapitalizationTypeAllCharacters, 所有字母都大写
} UITextAutocapitalizationType;
*/

//设置return键

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

//设置键盘外观

self.textField.keyboardAppearance=UIKeyboardAppearanceDefault;
/*
typedef enum {
    UIKeyboardAppearanceDefault, 默认外观,浅灰色
    UIKeyboardAppearanceAlert,   深灰 石墨色

} UIReturnKeyType
 */

pragma mark - 访问代理

//设置代理 用于实现协议, 需要先签写

self.textField.delegate = self;

/* 代理详情请看 “#pragma mark * 访问代理” /

/* 创建另一个对象 */

self.textField2 = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 150, 30)];
self.textField2.placeholder = @"OK";
self.textField2.borderStyle = UITextBorderStyleRoundedRect;
self.textField2.textAlignment = 1;
self.textField2.delegate = self;
[self.window addSubview:self.textField2];


return YES;

}

pragma mark ** 访问代理

//管理编辑

/* 文本框应该开始编辑 BOOL值 */
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
// return NO; /* 如果返回值为NO, 相当于禁止编辑 */
return YES;
}

/* 文本框已经开始编辑 */
- (void)textFieldDidBeginEditing:(UITextField *)textField
{

}

/* 文本框应该结束编辑 BOOL值 */
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
return YES;
}

/* 文本框已经结束编辑 */
- (void)textFieldDidEndEditing:(UITextField *)textField
{

}

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

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

  • (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
    if (self.textField == textField) {
    [self.textField2 becomeFirstResponder]; /* return键或回车键, 自动跳到下一个输入文本框 */
    } else if (self.textField2 == textField)
    {
    [self.textField2 resignFirstResponder]; /* 按return键或回车键, 键盘往下收 becomeFirstResponder */
    }
    return YES;
    }

/* 点击空白区域回收键盘 */
- (void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event
{
[self.window endEditing:YES];
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值