iOS开发基础-UITextView&UITextField

谭学名
2023-12-01
//UITextView 文本显示滚动试图,既可以显示文字,也可以编辑文字 继承自UIScrollView  是一个滚动试图
    //这个空间带有 UIScrollView中的方法 属性以及 Label中的一些方法和属性
    UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 300, 400)];

    textView.backgroundColor = [UIColor redColor];

    textView.font = [UIFont systemFontOfSize:33];
    textView.text = @"默认";
    textView.textColor = [UIColor greenColor];
    //隐藏 横竖滚动条
    textView.showsHorizontalScrollIndicator = NO;
    textView.showsVerticalScrollIndicator = NO;
UITextField

//用于用户输入文本信息(类似于C scanf)的UI视图

- (void)viewDidLoad //一般情况下只被调用一次
{
    [super viewDidLoad];

    //代码写在这里
    self.view.backgroundColor = [UIColor orangeColor];
    //视图控件的添加写在下面
    //创建一个UITextField实例,(文本框),如果没有后续人为处理的话,不支持多行显示.
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 30, 300,50)];

    //设置文本框的边框风格,设置为圆角矩形边框风格,默认风格为UITextBorderStyleNone
    textField.borderStyle = UITextBorderStyleRoundedRect;
        /*
            UITextBorderStyleNone,
            UITextBorderStyleLine,
            UITextBorderStyleBezel,
            UITextBorderStyleRoundedRect //圆角
            */

    //设置文本框的文字字体
    textField.font = [UIFont boldSystemFontOfSize:18];

    //文本框默认的提示文字,在我们真正输入文字时消失
    textField.placeholder = @"请输入内容";

    //YES,文字内容会显示成暗文 ,默认为NO,一般将文本框作为输入密码用途的时候,设置此属性
    textField.secureTextEntry = YES;

    //文本框文本内容的竖直方向的对齐方式,设置为竖直方向居中显示
    textField.contentVerticalAlignment =
    UIControlContentVerticalAlignmentCenter;

    //设置清除按钮的显示,默认为UITextFieldViewModeNever
    textField.clearButtonMode =UITextFieldViewModeWhileEditing;
            /*
            UITextFieldViewModeNever,
            UITextFieldViewModeWhileEditing,
            UITextFieldViewModeUnlessEditing,
            UITextFieldViewModeAlways
            */
    //当开始输入的时候清除里面的内容
    field.text = @"默认内容";
    field.clearsOnBeginEditing = YES;
    textField.backgroundColor = [UIColor cyanColor];
    //设置了borderStyle 背景图片无效果
    text.background = [UIImage imageNamed:@"textfield"];

对键盘相关的设置---在项目开发中比较常用

    //设置键盘的样式keyboardType属性
    //纯数字键盘
    textField.keyboardType = UIKeyboardTypeNumberPad;
            /*
            UIKeyboardTypeDefault,            
            UIKeyboardTypeASCIICapable,       
            UIKeyboardTypeNumbersAndPunctuation,
            UIKeyboardTypeURL,                 
            UIKeyboardTypeNumberPad,//数字键盘
            UIKeyboardTypePhonePad,      
            UIKeyboardTypeNamePhonePad,         
            UIKeyboardTypeEmailAddress,//邮箱
            UIKeyboardTypeDecimalPad 
            UIKeyboardTypeTwitter 
            UIKeyboardTypeWebSearch 
            UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
            */

    //设置键盘右下角的返回按钮的样式,利用returnKeyType属性,根据不用的使用情景,设置不同的返回按钮样式
    textField.returnKeyType = UIReturnKeyGo;
        /*
            UIReturnKeyDefault,
            UIReturnKeyGo,
            UIReturnKeyGoogle,
            UIReturnKeyJoin,
            UIReturnKeyNext,
            UIReturnKeyRoute,
            UIReturnKeySearch,
            UIReturnKeySend,
            UIReturnKeyYahoo,
            UIReturnKeyDone,
            UIReturnKeyEmergencyCall,
            UIReturnKeyContinue NS_ENUM_AVAILABLE_IOS(9_0),
            */

    //让textField成为第一响应者,textField会自动将它的键盘调出来.
    [textField becomeFirstResponder];
    textField.delegate = self;
}
    //不常用  改变键盘样式
    textField.keyboardAppearance = UIKeyboardAppearanceDark;
    /*
     UIKeyboardAppearanceDefault,    
     UIKeyboardAppearanceDark
     UIKeyboardAppearanceLight
     UIKeyboardAppearanceAlert = UIKeyboardAppearanceDark,  // D
     */
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];

左侧头试图 用于调制光标开始位置
    textField.leftView = view;
    textField.leftViewMode = UITextFieldViewModeAlways;
    view.backgroundColor = [UIColor redColor];
    /*
     UITextFieldViewModeNever,
     UITextFieldViewModeWhileEditing,
     UITextFieldViewModeUnlessEditing,
     UITextFieldViewModeAlways
     */
//    textField.rightView

    //设置第一响应者 textField会进入编辑状态 键盘自动弹起
    [textField becomeFirstResponder];
    //注销第一响应者
    [textField resignFirstResponder];

键盘顶部添加视图
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
    headView.backgroundColor = [UIColor redColor];
    //自定义键盘
    //textField.inputView = headView;

    //二级键盘
    textField.inputAccessoryView = headView;
 ---UITextFieldDelegate---

//textField已经开始编辑  开始编辑的时候会调用一次
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    NSLog(@"已经开始编辑");
    textField.background = [UIImage imageNamed:@"textfield_hl"];
}

//textField已经结束编辑
- (void)textFieldDidEndEditing:(UITextField *)textField{
    NSLog(@"已经结束编辑");
    textField.background = [UIImage imageNamed:@"textfield"];
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
    //默认YES 可以清空textField里的内容
    return YES;
}

//键盘右下角的reture能否被点击
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    //取消第一响应者 收起键盘
//    [textField resignFirstResponder];

    //收起键盘
    [self.view endEditing:YES];

    return YES;
}





******************************************************





//  ViewController.m
//  总结-UITextField

#pragma mark - 系统的textField的基础属性

- (void)createTextField1{
    //UITextField文本输入框视图
    //继承关系:UITextField -> UIControl -> UIView -> UIResponder ->NSObject
    UITextField * textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 40, 280, 40)];

    /***************常用属性*******************/
    //1.设定边框
    textField.borderStyle = UITextBorderStyleNone;
    //类型UITextBorderStyle 枚举
    /*
     UITextBorderStyleNone,//无边框
     UITextBorderStyleLine,//线性边框
     UITextBorderStyleBezel,//尖角矩形
     UITextBorderStyleRoundedRect//圆角矩形
     */

    //2.设置占位符 当没有文字,显示占位符
    textField.placeholder = @"请输入用户名....";

    //3.设置背景图片  注:设置背景颜色最好把边框样式(borderStyle)设置成无边框(UITextBorderStyleNone)
    textField.background = [UIImage imageNamed:@"textfield"];

    //4.设置清除健的模式
    textField.clearButtonMode = UITextFieldViewModeUnlessEditing;
    //类型UITextFieldViewMode 枚举
    /*
     UITextFieldViewModeNever,//一致不显示
     UITextFieldViewModeWhileEditing,//当编辑的时候显示
     UITextFieldViewModeUnlessEditing,//不编辑的时候显示
     UITextFieldViewModeAlways//在编辑文字的时候总出现
     */

    //5.设置密文输入 默认NO
    textField.secureTextEntry = YES;

    //6.设置每次编辑文字,原有文字消失
    textField.clearsOnBeginEditing = YES;

    //7.设置当前文本框是第一响应
    [textField becomeFirstResponder];
    //注:一个视图控制器上可能有多个可编辑空间,哪个正在编辑,哪个是当前第一响应,通过这个方法可以直接后台开启第一响应。

    //8.注销第一响应
    [textField resignFirstResponder];

    //9.键盘的类型
    textField.keyboardType = UIKeyboardTypeDefault;
    //类型UIKeyboardType 枚举
    /*
     UIKeyboardTypeDefault,//默认
     UIKeyboardTypeNumberPad,//数字键盘
     UIKeyboardTypeEmailAddress,//Email键盘
     UIKeyboardTypePhonePad,//电话键盘
     UIKeyboardTypeNamePhonePad,//即可输入文字,又可输入电话键
     UIKeyboardTypeASCIICapable,//能够顺利输入ASCII键盘
     UIKeyboardTypeNumbersAndPunctuation,//能够输入数字和标点
     UIKeyboardTypeURL,//适合输入网址的键盘
     UIKeyboardTypeDecimalPad//数字键盘 带小数点
     UIKeyboardTypeTwitter//推特键盘 微博键盘
     UIKeyboardTypeWebSearch//网页搜索键盘
     UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,//其实默认键盘可以输入上述所有键盘能输入的内容
     */

    //10.修改KeyBoard的return键的样式 应先将键盘样式设为default
    textField.returnKeyType = UIReturnKeyGoogle;
    //类型UIReturnKeyType 枚举
    /*
     UIReturnKeyDefault,
     UIReturnKeyGo,
     UIReturnKeyGoogle,
     UIReturnKeyJoin,
     UIReturnKeyNext,
     UIReturnKeyRoute,
     UIReturnKeySearch,
     UIReturnKeySend,
     UIReturnKeyYahoo,
     UIReturnKeyDone,
     UIReturnKeyEmergencyCall,
     UIReturnKeyContinue,
     */

    UIImageView * leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"btn_home_h"]];
    //注:x、y设与不设无所谓
    leftView.frame = CGRectMake(0, 0, 40, 50);
    //9.设置左视图 注:类型是UIView 所以UIView的子类都可以作为它的左视图
    textField.leftView = leftView;
    //10.设置左视图显示模式  同上:类型UITextFieldViewMode
    textField.leftViewMode = UITextFieldViewModeAlways;

    UIImageView * rightView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"btn_home_h"]];
    //x、y设与不设无所谓
    rightView.frame = CGRectMake(0, 0, 40, 50);
    //10.设置右视图  注:同一个试图不能即作为textField左视图又作为右视图
    textField.rightView = rightView;
    //11.设置右视图显示模式  同上:类型UITextFieldViewMode
    textField.rightViewMode = UITextFieldViewModeAlways;

    //注:x,y,width都不用管,只需要设置高度
    UIView *accessoryView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
    accessoryView.backgroundColor = [UIColor redColor];
    //12.设置弹出二级键盘
    textField.inputAccessoryView = accessoryView;

    /***************和UILabel相似的属性*****************/
    //1.输入框内容 这样设置是默认内容 会覆盖占位内容
    textField.text = @"输入框内容";
    //2.设置文字字体
    textField.font = [UIFont systemFontOfSize:20];
    //3.设置字体颜色
    textField.textColor = [UIColor redColor];
    //4.设置字体对齐方式
    textField.textAlignment = NSTextAlignmentCenter;
    //5.设置自适应宽度,空间不足,可以缩小字体,但是空间过大,不会放大字体
    textField.adjustsFontSizeToFitWidth = YES;
    //6.设置最小字体,在自适应字体时生效
    textField.minimumFontSize = 5;

    /********************不常用属性********************/

    //1.设置是否纠错
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    //类型UITextAutocorrectionType 枚举
    /*
     UITextAutocorrectionTypeDefault,//按照默认设置
     UITextAutocorrectionTypeNo,//不提示
     UITextAutocorrectionTypeYes,//提示
     */

    //2.设置是否自动大写
    textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
    //类型UITextAutocapitalizationType 枚举
    /*
     UITextAutocapitalizationTypeNone,//不大写
     UITextAutocapitalizationTypeWords,//单词首字母大写
     UITextAutocapitalizationTypeSentences,//句子首字母大写
     UITextAutocapitalizationTypeAllCharacters,//全大写
     */
    textField.borderStyle = UITextBorderStyleRoundedRect;
    //3.自定义键盘样式
    UILabel *inputLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 280)];
    inputLabel.backgroundColor = [UIColor redColor];
    inputLabel.numberOfLines = 0;
    inputLabel.text = @"自定义键盘,你可以在上面放一些按钮,做成键盘的样式";
    textField.inputView = inputLabel;

    //4.设置不能点击状态下的背景图片
    textField.disabledBackground = [UIImage imageNamed:@"textfield_hl"];

    [self.view addSubview:textField];
}

#pragma mark - 重写方法的textField

- (void)createTextField2{
    //自己定义的UITextField   重写了UITextField的方法
    MyTextField * textField = [[MyTextField alloc] initWithFrame:CGRectMake(20, 100, 280, 40)];
    textField.borderStyle = UITextBorderStyleBezel;
    textField.placeholder = @"请输入....";
    textField.clearButtonMode = UITextFieldViewModeAlways;
    UIImageView * leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"btn_home_h"]];
    leftView.frame = CGRectMake(0, 0, 40, 50);
    textField.leftView = leftView;
    textField.leftViewMode = UITextFieldViewModeAlways;

    UIImageView * rightView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"btn_home_h"]];
    rightView.frame = CGRectMake(0, 0, 40, 50);
    textField.rightView = rightView;
    textField.rightViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:textField];
}

#pragma mark - 显示代理的textField
- (void)createTextField3{

    UITextField * textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 160, 280, 40)];

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

    textField.delegate = self;

    [self.view addSubview:textField];
}


#pragma mark - 协议方法 UITextFieldDelegate

//textField是否可以被编辑  在开始编辑的时候会被调用
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    NSLog(@"是否可以被编辑");
    //默认返回的YES
    if ([textField.text isEqualToString:@"Over"]) {
        return NO;
    }
    return YES;
}

//textField已经开始编辑  开始编辑的时候会调用一次
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    NSLog(@"已经开始编辑");
    textField.background = [UIImage imageNamed:@"textfield_hl"];
}

//textField已经结束编辑
- (void)textFieldDidEndEditing:(UITextField *)textField{
    NSLog(@"已经结束编辑");
    textField.background = [UIImage imageNamed:@"textfield"];
}

//textField里的内容可以被清空
- (BOOL)textFieldShouldClear:(UITextField *)textField{
    //默认YES 可以清空textField里的内容
    return YES;
}

//键盘右下角的reture能否被点击
- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    //取消第一响应者 收起键盘
    [textField resignFirstResponder];

    //收起键盘
    [self.view endEditing:YES];

    return YES;
}

//当textField中的文字个数试图改变,委托这个方法,返回YES 允许改变, 返回NO不许改变
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{//只要在输入 这个方法就会被实时调用
    NSLog(@"你正在输入....");
    //第二个参数:range的长度通常无用,range.loc表示光标的位置
    //第三个参数:string是新写入的字符串

    //拿到当前textField里的内容
    NSMutableString * str = [[NSMutableString alloc] initWithString:textField.text];
    //把新输入的字符串拼接到原来的后面
    [str insertString:string atIndex:range.location];

    //当textField中的文字长度大于等于6的时候 不让继续编辑
    return str.length <= 6;
}
@end
 类似资料: