UI-UITextField UIButton
发布日期:2022-02-08 18:03:23 浏览次数:52 分类:技术文章

本文共 5327 字,大约阅读时间需要 17 分钟。

1. UITextField:

UITextField的属性:(部分)

文本显示:

1. 有UILable的一堆属性:text font textColor textAlignment

2. placeholder:占位字符串

输入控制:

3. enabled:是否允许在输入框内输入东西

4. secureTextEntry:密文输入

5. keyboardType:弹出键盘的样式

6. returnKeyType:return键的样式

7. inputView:自定义  输入的视图  默认是键盘

8. inputAccessoryView:输入视图上方的辅助视图

外观控制:

9. borderStyle:边框样式

10. clearButtonMode:清除按钮(就是后面那个小叉)

11. leftView:输入框的左视图

12. leftViewMode:输入框的左视图的显示模式

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];    _window.backgroundColor = [UIColor whiteColor];    [_window makeKeyAndVisible];        UIView *view = [[UIView alloc] initWithFrame:_window.bounds];    [_window addSubview:view];    [view release];        #pragma UITextField:输入框     UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 50, 200, 30)];    [view addSubview:textField];    // 属性enabled:YES能输入   NO不能输入内容    textField.enabled = YES;    //    textField.text = @"haha"; // 属性text:默认的文本   例如:记住密码 记住账号 // 属性placeholder:占位文本  (提示语)    textField.placeholder = @"请输入用户名~ ~ ";    // 属性:borderStyle textField的边框样式    textField.borderStyle = UITextBorderStyleRoundedRect;    // 属性:clearButtonMode,textField的清空按钮    textField.clearButtonMode = UITextFieldViewModeAlways;        textField.secureTextEntry = YES; //  密文输入    // 属性:keyboardType 控制弹出的键盘的样式    textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;// 这个是数字和标点符号    // return键的样式(return键长得是什么样子)    textField.returnKeyType = UIReturnKeyYahoo;        UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];    leftView.backgroundColor = [UIColor orangeColor];// leftView左视图  一定要设置leftViewMode才能显示出来  也有leftRight这个属性    textField.leftView = leftView;    textField.leftViewMode = UITextFieldViewModeAlways;        UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 30)];    inputView.backgroundColor = [UIColor yellowColor];// 键盘上方的辅助视图    textField.inputAccessoryView = inputView;        UIView *inView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];    inView.backgroundColor =[UIColor purpleColor];    // 把键盘替换了  inputView就是键盘    textField.inputView = inView;        #pragma delegate:遵守协议--设置代理--实现方法// 回收键盘之----设置代理 需要遵守协议:UITextFieldDelegate    textField.delegate = self;    [textField release];    [leftView release];    [inputView release];    [inView release];    return YES;}// 回收键盘之----实现方法:- (BOOL)textFieldShouldReturn:(UITextField *)textField{    // 释放第一响应者,回收键盘    [textField resignFirstResponder];    return YES;}

2. UIButton:

1. 按钮不需要释放,使用的是类方法创建的button

2. addTarget: action: forControlEvents: 添加事件 执行action方法

3. removeTarget: action: forControlEvents: 移除按钮的点击事件

4. setTitle: forState     setTitleColor: forState

5. button1.titleLabel.font = [UIFont systemFontOfSize:20];   改变button的字体大小

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];    _window.backgroundColor = [UIColor whiteColor];    [_window makeKeyAndVisible];        UIView *backView = [[UIView alloc] initWithFrame:_window.bounds];    backView.backgroundColor = [UIColor whiteColor];    [_window addSubview:backView];#pragma UIButton:初始化的时候有点不一样        // 初始化的时候和View不一样了~ ~ ~    _button = [UIButton buttonWithType:UIButtonTypeSystem];// 便利构造器  不用释放    _button.frame = CGRectMake(50, 50, 100, 50);    _button.backgroundColor = [UIColor cyanColor];    [backView addSubview:_button];// 设置抬头,没有.text  button是集合控件 一般都是Normal    [_button setTitle:@"BUT" forState:UIControlStateNormal];    //    [_button setTitle:@"aaaa" forState:UIControlStateHighlighted]; // 长按才会显示    //    [_button setTitle:@"什么鬼" forState:UIControlStateSelected];//    _button.selected = YES;// selected是button的属性    // 设置圆角 如果值等于button(正方形的时候)的宽一半 就是个圆    _button.layer.cornerRadius = 25;    //边框的颜色    _button.layer.borderColor = [UIColor blackColor].CGColor;    // 边框的粗细    _button.layer.borderWidth = 1.0;        #pragma  修改button背景色的方法:    //    修改button背景色的方法一//    _button.tag = 101;//    [_button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];        // 修改button背景色的方法二    [_button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];        // 修改button背景色的方法三    [_button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];// 给button添加事件的            //  设置button抬头颜色    [_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];    [_button setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal]; // 设置阴影颜色 然并卵    [backView release];    return YES;}#pragma button的响应事件://  方法一:利用tag- (void)buttonAction{    [((UIView *)_window.subviews[0]) viewWithTag:101].backgroundColor = [UIColor greenColor];}  方法二:把button设置为属性//- (void)buttonAction{//    _button.backgroundColor = [UIColor redColor];//}// 方法三:传参数,谁调用这个方法,传过来的就是谁- (void)buttonAction:(UIButton *)button{  // 这个方法  点击一下就可以换一个随机颜色    CGFloat num1 = arc4random()%256;    CGFloat num2 = arc4random()%256;    CGFloat num3 = arc4random()%256;    button.backgroundColor = [UIColor colorWithRed:num1/255.0 green:num3/255.0 blue:num2/255.0 alpha:1.0];}

总结:

转载地址:https://blog.csdn.net/Evelynzn/article/details/49101563 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:数据持久化
下一篇:UI-事件处理

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年03月28日 08时27分25秒