IOS疯狂基础之UIButton

有些时候我们想让UIButton的title居左对齐,我们设置

btn.textLabel.textAlignment UITextAlignmentLeft

是没有作用的,我们需要设置

btn.contentHorizontalAlignment UIControlContentHorizonAlignmentLeft;

但是问题又出来,此时文字会紧贴到做边框,我们可以设置

btn.contentEdgeInsets UIEdgeInsetsMake(0,10, 0, 0);

使文字距离做边框保持10个像素的距离。

=======================================================

设置UIButton上字体的颜色设置UIButton上字体的颜色,不是用:

[btn.titleLabel setTextColor:[UIColorblackColor]];

btn.titleLabel.textColor=[UIColor redColor];

而是用:

[btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];

    UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    [btn addTarget:selfaction:@selector(buttonDown:)forControlEvents:UIControlEventTouchDown];//设置事件有冒号的方法带参数

    [btn addTarget:selfaction:@selector(btnUp)forControlEvents:UIControlEventTouchUpInside];

    [btn setTitle:@"这就去拿" forState:UIControlStateNormal];//设置标题

    [btn setTitleColor:[UIColorwhiteColor]forState:UIControlStateNormal];

    [btn setTitleColor:[UIColorwhiteColor]forState:UIControlEventTouchDown];

    btn.frame = CGRectMake(80.0,210.0,160.0, 40.0);//大小位置

    [btn setBackgroundImage:[UIImageimageNamed:@"btn1.jpg"]forState:UIControlStateNormal];//设置背景图片

    [btn setBackgroundImage:[UIImageimageNamed:@"btn2.jpg"]forState:UIControlEventTouchDown];//设置按下去时的图片

#import <QuartzCore/QuartzCore.h>

 //设置登录按钮边框

   loginBtn.tag =2;

   CALayer * downButtonLayer = [loginBtnlayer];

    [downButtonLayersetMasksToBounds:YES];

    [downButtonLayersetCornerRadius:10.0];

    [downButtonLayersetBorderWidth:1.0];

    [downButtonLayer setBorderColor:[[UIColorcolorWithRed:7/255.0green:98/255.0blue:182/255.0alpha:1]CGColor]];

    [self.viewinsertSubview:loginBtnatIndex:loginBtn.tag];



待续。。。

-(void)buttonDown:(id)sender{

    //UIButton* btn = (UIButton*)sender;

    NSLog(@"Button pushed down");

}

-(void)btnUp{

    NSLog(@"btn is up");

}


其他:

UIButton状态:

UIControlStateNormal          // 正常状态   
UIControlStateHighlighted     // 高亮状态   
UIControlStateDisabled        // 禁用状态    
UIControlStateSelected        // 选中状态    
UIControlStateApplication     //     
UIControlStateReserved        // 保留状态
 

UIButton类型:

UIButtonTypeCustom            //自定义类型
添加图片:  灰色背景颜色:
UIButtonTypeRoundedRect       //圆角类型
UIButtonTypeDetailDisclosure   //细节展示按钮
UIButtonTypeInfoLight          //浅色背景的信息按钮
UIButtonTypeInfoDark           //暗色背景的信息按钮
UIButtonTypeContactAdd         // 添加按钮

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值