温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iOS UITextField的基本用法

发布时间:2020-05-24 10:19:32 来源:网络 阅读:1198 作者:大头狼小鬼 栏目:移动开发

 UITextField *textField = [[UITextField alloc] init];//初始化

    textField.userInteractionEnabled = YES;//是否可用

    textField.text = @"UITextField"; //文字

    textField.delegate = self; //代理

    textField.frame = CGRectMake(100, 100, 100, 40); //大小和位置

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

    textField.placeholder = @"UITextField";//提示字符

    [textField setBorderStyle:UITextBorderStyleRoundedRect]; //外框类型UITextBorderStyleRoundedRect枚举类型

    textField.secureTextEntry = YES; //密码框

    textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X

    UIImageView *imgv=[[UIImageView alloc] initWithImage:[UIImage p_w_picpathNamed:@"right.png"]];

    textField.rightView=imgv;

    textField.rightViewMode = UITextFieldViewModeAlways; //右侧加图片

    textField.font = [UIFont systemFontOfSize:14.0f];//文字的大小

    textField.font = [UIFont boldSystemFontOfSize:14.0f];//文字加粗

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


    textField.clearsOnBeginEditing = YES;    //再次编辑就清空


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

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

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

    textField.backgroundColor = [UIColor grayColor];//背景颜色

    //placeholder 颜色

    //第一种

    UIColor *color = [UIColor whiteColor];

    textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"用户名" attributes:@{NSForegroundColorAttributeName: color}];

    //第二种

    [textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];//_placeholderLabel.textColor这个不可以修改

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

    //文字上下居中

    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

    //文字左右居中

    textField.textAlignment = NSTextAlignmentCenter;

  textField.keyboardAppearance=UIKeyboardAppearanceDefault;  //键盘外观


    //UITextField左边的距离

    CGRect frame = [textField frame];

    frame.size.width = 15;

    UIView *leftview = [[UIView alloc] initWithFrame:frame];

    textField.leftViewMode = UITextFieldViewModeAlways;  //左边距为15pix

    textField.leftView = leftview;

    [textField becomeFirstResponder];//成为第一响应者

    [self.view addSubview:textField];


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI