UIButton使用,下划线,文字图片按钮等

UIButton是iOS开发中非常重要的一个基础控件,很多地方都需要用到它,所以下面来写一下对UIButton的一些认识!
那么让我们来创建一个UIButton吧,直接上代码!
1、文字按钮

- (void)addTitleBtn {
    /*
     *  prama1:UIButtonTypeSystem,系统类型,不可用图片
     *  prama2:UIButtonTypeCustom,自定义类型,可用于图片按钮
     */
    //1、初始化btn,设置button类型
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    //2、设置frame
    btn.frame = CGRectMake(100, 100, 100, 30);
    //3、设置btn上的文字
    [btn setTitle:@"系统按钮" forState:UIControlStateNormal];
    //4、设置背景颜色
    btn.backgroundColor = [UIColor redColor];
    //5、设置按钮文字颜色
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //6、设置按钮文字的大小
    btn.titleLabel.font = [UIFont systemFontOfSize:20];
    //7、设置按钮文字的位置
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    //8、设置视图颜色,不一定有效,对btn来说就没有效果
    btn.tintColor = [UIColor yellowColor];
    //9、子视图的位置
    btn.contentEdgeInsets = UIEdgeInsetsMake(10, 10, 0, 0);
    //10、没看出来是干嘛的
    btn.reversesTitleShadowWhenHighlighted = NO;
    //点击时的特效,会有一个亮光,默认为NO
    btn.showsTouchWhenHighlighted = YES;
    
    [self.view addSubview:btn];
}```
System类型的button是不能设置图片的,下面来看一下图片按钮吧!

2、图片按钮
  • (void)addImageBtn {

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(40, 150, 80, 80);
    //正常按钮图片,图片按图片正常大小居中显示
    [btn setImage:[UIImage imageNamed:@"friends_lose"] forState:UIControlStateNormal];
    //按压时按钮图片
    [btn setImage:[UIImage imageNamed:@"friends_normal"] forState:UIControlStateHighlighted];
    [self.view addSubview:btn];

UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
btn1.frame = CGRectMake(140, 150, 80, 80);
//按钮有多大,图片就显示多大
[btn1 setBackgroundImage:[UIImage imageNamed:@"friends_lose"] forState:UIControlStateNormal];
[btn1 setBackgroundImage:[UIImage imageNamed:@"friends_normal"] forState:UIControlStateHighlighted];
//设置btn的边线宽和颜色
btn1.layer.borderWidth = 1.0;
btn1.layer.borderColor = [UIColor blackColor].CGColor;
//设置btn边线圆角
btn1.layer.cornerRadius = 10;
[self.view addSubview:btn1];

UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
btn2.frame = CGRectMake(240, 150, 80, 80);
[btn2 setImage:[UIImage imageNamed:@"friends_lose"] forState:UIControlStateNormal];
[btn2 setImage:[UIImage imageNamed:@"friends_normal"] forState:UIControlStateHighlighted];
//设置图片位置
btn2.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 0, 0);
//当按钮高亮时,图片变暗,默认YES
btn2.adjustsImageWhenHighlighted = NO;
//当按钮禁用时,图片变暗,默认YES
btn2.adjustsImageWhenDisabled = NO;

[self.view addSubview:btn2];

}```
btn.layer是btn的图层,可以用来设置btn的边线等!

3、图片文字按钮

- (void)addTitleAndImageBtn {
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(40, 250, 80, 80);
    btn.layer.borderWidth = 1.0;
    btn.layer.borderColor = [UIColor blackColor].CGColor;
    //正常按钮图片,图片按图片正常大小显示
    [btn setImage:[UIImage imageNamed:@"friends_lose"] forState:UIControlStateNormal];
    //按压时按钮图片
    [btn setImage:[UIImage imageNamed:@"friends_normal"] forState:UIControlStateHighlighted];
    btn.imageEdgeInsets = UIEdgeInsetsMake(-20, 10, 0, 0);
    
    [btn setTitle:@"图片加文字按钮" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    btn.titleEdgeInsets = UIEdgeInsetsMake(60, -50, 0, 0);
    btn.titleLabel.font = [UIFont systemFontOfSize:12];
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    //画出titleLabel的边线看看label在哪里吧!
    btn.titleLabel.layer.borderColor = [UIColor blackColor].CGColor;
    btn.titleLabel.layer.borderWidth = 1.0;
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.view addSubview:btn];
}```

4、在button下加下划线
  • (void)addLineBtn {

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    //2、设置frame
    btn.frame = CGRectMake(140, 250, 100, 30);
    //3、设置btn上的文字
    [btn setTitle:@"按钮加下划线" forState:UIControlStateNormal];
    NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc] initWithString:@"按钮加下划线"];
    NSRange strRange1 = {0,[str1 length]};
    //设置下划线范围
    [str1 addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange1];
    //设置下划线颜色
    [str1 addAttribute:NSUnderlineColorAttributeName value:ILColor(0, 122, 25) range:strRange1];
    //将下划线绘制到按钮上
    [btn setAttributedTitle:str1 forState:UIControlStateNormal];
    [self.view addSubview:btn];
    }```

以上的代码大部分都写了注释啦,这里就不多解释啦,希望对你有帮助,可以在下面提提意见哈!!!

你可能感兴趣的:(UIButton使用,下划线,文字图片按钮等)