UI基础知识 -- UITextView UIButton

UITextView

初始化

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 50)];

属性

1.背景颜色

textField.backgroundColor = [UIColor grayColor];

2.文本内容

textField.text = @"haha";

3.占位字符串
只有当没有输入的字时,才会显示(有text属性就显示不出来)

textField.placeholder = @"电话/邮箱";

4.文本字体颜色

textField.textColor = [UIColor blackColor];

5.对齐方式

textField.textAlignment = NSTextAlignmentLeft;

6.字体大小

textField.font = [UIFont systemFontOfSize:15];

7.边框样式

textField.borderStyle = UITextBorderStyleRoundedRect;

8.是否允许输入

textField.enabled = NO;

9.是否在开始编辑时将text内容清空

textField.clearsOnBeginEditing = NO;

10.密文输入

textField.secureTextEntry = YES;

11.弹出键盘的类型

textField.keyboardType = UIKeyboardTypeDefault;

12.弹出键盘右下角return按钮 类型

textField.returnKeyType = UIReturnKeyDone;

13.自定义键盘输入视图

    UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 300)];
    inputView.backgroundColor = [UIColor grayColor];
    textField.inputView = inputView;
    [inputView release];

14.自定义键盘的辅助视图

    UIView *inputAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 50)];
    inputAccessoryView.backgroundColor = [UIColor brownColor];
    textField.inputAccessoryView = inputAccessoryView;
    [inputAccessoryView release];

15.清楚按钮模式

textField.clearButtonMode = UITextFieldViewModeWhileEditing;

16.输入框左视图

    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 50)];
    leftView.backgroundColor = [UIColor blueColor];
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.leftView = leftView;
    [leftView release];

17.输入框右视图

    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 50)];
    rightView.backgroundColor = [UIColor brownColor];
    textField.rightViewMode = UITextFieldViewModeAlways;
    textField.rightView = rightView;
    [rightView release];

主要的协议方法

AppDelegate类可以遵守一个UITextFieldDelegate协议

然后在其.m 文件中实现其中某个方法,其中最常用的是:

// 实现协议中的方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    // 键盘回收
    // 取消第一响应者
    [textField resignFirstResponder];

    return YES;
}

在设置代理时可以这么写:

// 设置代理(方法在哪里实现的 就把谁设置成代理)
    textField.delegate = self;

如果将各UITextField设置tag

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    UITextField *temp = (UITextField *)[self.window viewWithTag:textField.tag + 1];
// 成为第一响应者
    [temp becomeFirstResponder];
    if (textField.tag == 102) {
        [textField resignFirstResponder];
        [[self.window viewWithTag:textField.tag - 2] becomeFirstResponder];
    }

UIButton

初始化

   UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100, 100, 100, 100);
    button.backgroundColor = [UIColor grayColor];

属性

1.给按钮的某个状态设置标题

    [button setTitle:@"普通" forState:UIControlStateNormal];
    [button setTitle:@"高亮" forState:UIControlStateHighlighted];
    [button setTitle:@"选中" forState:UIControlStateSelected];

2.给某个标题设置一下颜色

    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];
    [button setTitleColor:[UIColor brownColor] forState:UIControlStateSelected];

3.给button添加一个前景图片

// 创建一张图片
// 如果不是png格式的图片 需要把后缀加上
    UIImage *image1 = [UIImage imageNamed:@"Normal"];
    [button setImage:image1 forState:UIControlStateNormal];

    UIImage *image2 =[UIImage imageNamed:@"Highlighted"];
    [button setImage:image2 forState:UIControlStateHighlighted];

    UIImage *image3 = [UIImage imageNamed:@"Selected"];
    [button setImage:image3 forState:UIControlStateSelected];

4.给button添加一个背景图片

    UIImage *image1 = [UIImage imageNamed:@"Normal"];
    [button setBackgroundImage:image1 forState:UIControlStateNormal];

    UIImage *image2 = [UIImage imageNamed:@"Highlighted"];
    [button setBackgroundImage:image2 forState:UIControlStateHighlighted];

    UIImage *image3 = [UIImage imageNamed:@"Selected"];
    [button setBackgroundImage:image3 forState:UIControlStateSelected];

UIButton的常用方法

// 给button添加一个方法
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

其中buttonClick: 方法 是需要我们自己写的方法

- (void)buttonClick:(UIButton *)Button
{

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值