iOS开篇——UI之UITextField

创建文本输入框

    UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 50, 250, 40)];

设置边框样式

    textField.borderStyle = UITextBorderStyleRoundedRect;
    /*
     typedef NS_ENUM(NSInteger, UITextBorderStyle) {
     UITextBorderStyleNone, 无效果
     UITextBorderStyleLine, 线性边框 有阴影
     UITextBorderStyleBezel,
     UITextBorderStyleRoundedRect 圆角矩形
     };
     */

设置text相关

    textField.text = @"2002年的第一场雪";
    //设置字体相关
    textField.font = [UIFont systemFontOfSize:20];
    textField.textColor = [UIColor greenColor];
    //设置自适应
    textField.adjustsFontSizeToFitWidth = YES;
    //设置居中
//    textField.textAlignment = NSTextAlignmentCenter;

设置提示语

    //设置提示语
    textField.placeholder = @"请输入";

    //设置是否清除 开始编辑时
//    textField.clearsOnBeginEditing = YES;

设置/取消第一响应者

    //点击输入框 让输入框成为了第一响应者
    //所谓第一响应者  就是即将编辑的控件  要操作触摸的控件
    [textField becomeFirstResponder];
    //取消成为第一响应者
    [textField resignFirstResponder];

交互是否开启

    //取消交互是否开启
    textField.userInteractionEnabled = YES;

设置清除按钮

    //设置清除按钮
    textField.clearButtonMode = UITextFieldViewModeAlways;
    /*
     typedef NS_ENUM(NSInteger, UITextFieldViewMode) {
     UITextFieldViewModeNever,  从不
     UITextFieldViewModeWhileEditing,  编辑时
     UITextFieldViewModeUnlessEditing,  不编辑时显示
     UITextFieldViewModeAlways  一直显示
     };
     */

设置左右两侧view

    //**********同一个view不能同时设置为左右图片
//    左侧view
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    view.backgroundColor = [UIColor blueColor];
    textField.leftView = view;
    textField.leftViewMode = UITextFieldViewModeAlways;
//    右侧view
    UIView * view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    textField.rightView = view1;
    view1.backgroundColor = [UIColor greenColor];
//    textField.rightViewMode = UITextFieldViewModeAlways ;

设置键盘样式

//设置键盘样式
    textField.keyboardType = UIKeyboardTypeDefault;
    /*
     typedef NS_ENUM(NSInteger, UIKeyboardType) {
     UIKeyboardTypeDefault,                // Default type for the current input method.
     UIKeyboardTypeASCIICapable,           // Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active
     UIKeyboardTypeNumbersAndPunctuation,  // Numbers and assorted punctuation.
     UIKeyboardTypeURL,                    // A type optimized for URL entry (shows . / .com prominently).
     UIKeyboardTypeNumberPad,              // A number pad (0-9). Suitable for PIN entry.
     UIKeyboardTypePhonePad,               // A phone pad (1-9, *, 0, #, with letters under the numbers).
     UIKeyboardTypeNamePhonePad,           // A type optimized for entering a person‘s name or phone number.
     UIKeyboardTypeEmailAddress,           // A type optimized for multiple email address entry (shows space @ . prominently).
     UIKeyboardTypeDecimalPad NS_ENUM_AVAILABLE_IOS(4_1),   // A number pad with a decimal point.
     UIKeyboardTypeTwitter NS_ENUM_AVAILABLE_IOS(5_0),      // A type optimized for twitter text entry (easy access to @ #)
     UIKeyboardTypeWebSearch NS_ENUM_AVAILABLE_IOS(7_0),    // A default keyboard type with URL-oriented addition (shows space . prominently).

     UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated

     };
     */
    textField.returnKeyType =UIReturnKeyDone;
    /*
     typedef NS_ENUM(NSInteger, UIReturnKeyType) {
     UIReturnKeyDefault,
     UIReturnKeyGo,
     UIReturnKeyGoogle,
     UIReturnKeyJoin,
     UIReturnKeyNext,
     UIReturnKeyRoute,
     UIReturnKeySearch,
     UIReturnKeySend,
     UIReturnKeyYahoo,
     UIReturnKeyDone,
     UIReturnKeyEmergencyCall,
     UIReturnKeyContinue NS_ENUM_AVAILABLE_IOS(9_0),
     };
     */

触摸屏幕  取消第一响应者 退出编辑

//触摸屏幕 调用此方法
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITextField * textField = (id)[self.view viewWithTag:1];
    [textField resignFirstResponder];
//    [self.view becomeFirstResponder];
}

实现UITextFieldDelegate代理方法

#pragma mark - UITextFieldDelegate

//是否可以开始编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return YES;
}// return NO to disallow editing.

//已经开始编辑
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    NSLog(@"已经开始编辑");
}// became first responder

//是否可以结束编辑 yes可以  no不可以
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    return YES;
}// return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end

//已经结束编辑
- (void)textFieldDidEndEditing:(UITextField *)textField{
    //可在此进行保存草稿
    NSLog(@"已经结束编辑");
}// may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    return YES;
}// return NO to not change text

//触发此方法 返回可否清除
- (BOOL)textFieldShouldClear:(UITextField *)textField{
    return NO;
}// called when clear button pressed. return NO to ignore (no notifications)

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

}
//点击键盘上的return 执行此方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    return  YES;
}// called when ‘return‘ key pressed. return NO to ignore.
时间: 2024-12-24 02:51:37

iOS开篇——UI之UITextField的相关文章

iOS之UI学习-UITextField代理篇

</pre><pre name="code" class="objc">#import "ViewController.h" //签订代理协议 @interface ViewController ()<UITextFieldDelegate> @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //须知:U

ios开发UI篇—UITextfield

概述 UITextField在界面中显示可编辑文本区域的对象. 您可以使用文本字段来使用屏幕键盘从用户收集基于文本的输入.键盘可以配置许多不同类型的输入,如纯文本,电子邮件,数字等等.文本字段使用目标操作机制和委托对象来报告在编辑过程中所做的更改. 除了基本的文本编辑行为之外,还可以将叠加视图添加到文本字段以显示其他信息并提供其他可定位控件.您可以为诸如书签按钮或搜索图标等元素添加自定义叠加视图.文本字段提供内置的叠加视图来清除当前文本.自定义覆盖视图的使用是可选的. 属性和方法 初始化 UIT

iOS开发-UI (五)UITextField

UITextField使用 1.创建方式 例: UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)]; 2.常用方法和属性 1)边框样式 @property(nonatomic)  UITextBorderStyle   borderStyle; UITextBorderStyleNone                       没有边框,背景默认为透明 UITextBorderS

iOS学习UI之UITextfield

UITextField->UIControl->UIView 常用属性 1.图片对象转化为颜色对象 textField.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"DOVE 1"]]; 2.borderStyle 边框样式 textField.borderStyle = UITextBorderStyleNone; 3.contentVerticalAlignment 文本垂直

iOS开篇——UI之UIGestureRecogzier_手势

一.UITouch 1 //任何视图都可以触发此方法 2 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ 3 NSLog(@"视图被触摸了"); 4 } 5 6 - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ 7 NSLog(@"因意外

iOS开篇——UI之UITableView

1 #import "ViewController.h" 2 3 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> 4 { 5 //创建数据源 6 NSMutableArray * _dataArray; 7 8 } 9 10 @end 11 12 @implementation ViewController 13 14 - (void)viewDidLoad { 15 [supe

iOS开篇——UI之UITextView

创建UITextView //创建一个单例对象 存储_str字符串 NSUserDefaults * hd = [NSUserDefaults standardUserDefaults]; _str = [hd objectForKey:@"str"]; UITextView * textView = [[UITextView alloc]initWithFrame:CGRectMake(50, 100, 200, 200)]; textView.delegate = self; te

iOS开篇——UI之UAlertView(提示框)

创建提示框 //创建提示框 //标题 提示内容 代理对象 按钮 UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"警告" message:@"萨达姆已经做好战斗准备" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",@"不确定", nil]; 设置提示框样式

iOS开篇——UI之UISegmentedControl (分段选择器)

创建分段选择器 UISegmentedControl * sc = [[UISegmentedControl alloc]initWithFrame:CGRectMake(50, 100, 200, 30)]; [sc insertSegmentWithTitle:@"第一页" atIndex:0 animated:YES]; [sc insertSegmentWithTitle:@"第二页" atIndex:1 animated:YES]; [sc insertS