iOS开发 ----- UIButton

来源:互联网 发布:硬盘开盘数据恢复 编辑:程序博客网 时间:2024/06/07 03:03

UIButton

//创建 button 也可以用alloc init方式创建 button的类型为自定义类型    UIButton * button2 = [UIButton buttonWithType:UIButtonTypeCustom];//初始化两张图片    UIImage * image = [UIImage imageNamed:@"1.png"];    UIImage * bgImage = [UIImage imageNamed:@"map.png"];//设置button的前景图    [button2 setImage:image forState:UIControlStateNormal];//设置button的背景图    [button2 setBackgroundImage:bgImage forState:UIControlStateNormal];//设置button的位置大小    button2.frame = CGRectMake(0, 80, 375, 30);//设置点击的时候是否可以高亮    button2.showsTouchWhenHighlighted = YES;//添加到视图上    [self.view addSubview:button2];//初始化button            UIButton * button3 = [UIButton buttonWithType:UIButtonTypeCustom];    button3.frame = CGRectMake(0, 120, 375, 30);//设置button的标题    [button3 setTitle:@"按钮" forState:UIControlStateNormal];//设置高亮时的标题    [button3 setTitle:@"llalala" forState:UIControlStateHighlighted];//设置背景颜色    [button3 setBackgroundColor:[UIColor grayColor]];//设置button的字体的颜色    [button3 setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];//设置button是否可用    button3.enabled = YES;//设置button的唯一标示符    button3.tag = 101;//button的点击方法 自己写onClick方法即可    [button3 addTarget:self action:@selector(onClick:)forControlEvents:UIControlEventTouchUpInside];//其实button上边的字体相当于一个label,可以利用label的相关属性进行设置//设置圆角矩形    button3.layer.cornerRadius = 10;//设置边框的颜色    button3.layer.borderColor = [UIColor grayColor].CGColor;//设置边框的宽度    button3.layer.borderWidth = 3;//设置button上的字体的大小    button3.titleLabel.font = [UIFont fontWithName:@"Menlo" size:30];//设置字体的背景颜色    button3.titleLabel.backgroundColor = [UIColor redColor];//设置 图片的位置 上左下右    button.imageEdgeInsets = UIEdgeInsetsMake(10, 20, 10, 40);//设置 内容的位置 上左下右    button.contentEdgeInsets = UIEdgeInsetsMake(30, 10, 0, 0);//设置 文字的位置 上左下右    button.titleEdgeInsetsEdgeInsets = UIEdgeInsetsMake(30, 10, 0, 0);//把button添加到view上    [self.view addSubview:button3];
0 0
原创粉丝点击