zoukankan      html  css  js  c++  java
  • UITextField学习小结

    1. -UITextFiled 通常显示一行   -UITextView 可以输入很多数据

    当用户点击一个UITextField、UITextView 表名需要输入数据 点击的这个控件就会成为第一响应者firstResponder 系统就会自动弹出一个键盘

    becomeFirstResponder成为第一响应者:弹出键盘
    //弹出键盘

    [self.nameTextFiled becomeFirstResponder];

    resignFirstResponder取消第一响应者:隐藏键盘
    //隐藏键盘

    [self.nameTextFiled resignFirstResponder];

    keyboardType 改键盘类型

    -text 显示的内容
    -placeholder 默认提示内容 开始输入的时候就自动消失
    -background 设置背景图 当 borderStyle为roundRect的时候无效

    -borderStyle 外框的风格 none line bezel round

    UITextBorderStyleNone, UITextBorderStyleLine,

    UITextBorderStyleBezel, UITextBorderStyleRoundedRect

    //创建UITextFiled
    self.nameTextFiled = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 200, 50)];

    //设置边框的类型

    _nameTextFiled.borderStyle = UITextBorderStyleLine;

    //设置边框颜色

    _nameTextFiled.layer.borderColor = [UIColor greenColor].CGColor;

    _nameTextFiled.layer.borderWidth = 1;

    //设置默认提示内容

    _nameTextFiled.placeholder = @"请输入名字";

    //设置字的属性

    _nameTextFiled.textColor = [UIColor redColor];

    _nameTextFiled.textAlignment = NSTextAlignmentLeft;

    _nameTextFiled.font = [UIFont systemFontOfSize:16];

    //设置键盘的风格

    _nameTextFiled.keyboardType = UIKeyboardTypeDefault;

    [self.view  addSubview:_nameTextFiled];

    监听行为/事件 delegate

    UITextFieldDelegate //配置是否可以输人

    //当textField becomeFirstResponder之前会调用这个方法
    //YES 可以编辑 可以成为第一响应者

    //NO 不可以编辑 不能成为第一响应者

    - (BOOL)textFieldShouldBeginEditing: (UITextField *)textField{

    NSLog(@"是否可以开始编辑");

    return YES;

    }

    //开始编辑 点击输入框 即将开始输人内容
    - (void)textFieldDidBeginEditing: (UITextField *)textField{

    NSLog(@"开始编辑 textFieldDidBeginEditing "); }

    //配置是否可以取消第一响应者 是否可以停止输入内容
    //当textField resignFirstResponder之前会调用这个方法

    //YES 可以取消第一响应者
    //NO 可以取消 一直等待输入
    - (BOOL)textFieldShouldEndEditing: (UITextField *)textField{

    NSLog(@"是否可以停止输入 ");

    return YES; }

    //停止编辑
    - (void)textFieldDidEndEditing:(UITextField *)textField{

    NSLog(@"停止编辑 "); }

    //键盘上的return按钮被点击
    - (BOOL)textFieldShouldReturn: (UITextField *)textField{

    NSLog(@"键盘上的return键被点击 ");

    [self.nameTextFiled resignFirstResponder];

    return YES; }

    //实时监听textField上文本内容的改变

    //range
    //string 新输入的字符

    //即将 新输入的字符来拼接字符 注意 还没有拼接

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange: (NSRange)range replacementString: (NSString *)string{

    //原来显示的内容

    NSLog(@"改变之前的内容:%@", textField.text);

    //新的内容= string去替换text上range 范围的内容

    NSString *newStr = [textField.text stringByReplacingCharactersInRange:range withString:string];

    NSLog(@"即将显示的内容:%@", newStr);

    return YES; } 

  • 相关阅读:
    skywalking学习
    logstash使用
    AIO编程
    NIO编程
    NIO入门之BIO
    Akka Cluster之集群分片
    Akka Stream之Graph
    【Swift学习笔记-《PRODUCT》读书记录-实现自定义转场动画】
    CoreData 数据模型文件导出NSManagedObject时重复问题
    iOS图片压缩
  • 原文地址:https://www.cnblogs.com/ZhaoChenYang/p/9443517.html
Copyright © 2011-2022 走看看