iOS之按钮UIButton的常用属性及方法总结(一)

本文详细介绍了 UIButton 的属性设置方法,包括文字、图片、背景及布局调整等内容,并讲解了不同状态下的设置技巧。此外,还探讨了 UIButton 的点击事件处理及如何自适应布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.在设置button的属性时,用titleLabe设置不起作用,UIButton继承于UIControl,需要设置状态,

按钮中得label的frame默认是(0,0,0,0);hidden 的属性默认是YES;

2.button中的图片文字排列:默认时图片在左,文字在右   

注意:设置按钮文字的状态不能用UIControlStateHighLighted否则控制台会报警告   可以用选中状态的UIControlStateSelected


 // 实例化一个button

    UIButton *headerButton = [[UIButtonalloc]init];

    


    /**

     设置背景图片

     设置图片的时候,哪些一定要分状态

     title/titleColor image , backgroudImage

     

     button.titleLabel.font  // 所有状态下的文本大小

     */

 [btn setTitle:@"按钮"forState:UIControlStateNormal];


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

//设置按钮的图片时,UIControlStateHighlighted就相当于点击按钮时;



    [headerButton setBackgroundImage:[UIImageimageNamed:@"buddy_header_bg"]forState:UIControlStateNormal];

    [headerButton setBackgroundImage:[UIImageimageNamed:@"buddy_header_bg_highlighted"]

forState:UIControlStateHighlighted];

    

    // 设置图片

    [headerButton setImage:[UIImageimageNamed:@"buddy_header_arrow"]forState:UIControlStateNormal];

    

    // title 设置颜色

    [headerButton setTitleColor:[UIColorblackColor]

forState:UIControlStateNormal];

    

    // 设置button的水平对其方式

    headerButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;

    [self.collectBtnsetContentHorizontalAlignment:

UIControlContentHorizontalAlignmentLeft];//设置水平方向的位置居左

    [self.collectBtnsetContentVerticalAlignment:

UIControlContentVerticalAlignmentTop];//设置竖直方向的位置居上


//返回label的size大小

    CGSize ttitleSize = [self.collectBtn.titleLabel.text sizeWithAttributes:@{ NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Light" size:10] }];

    // 设置图片的内边距

    headerButton.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

    

    // 设置文本的内边距

    headerButton.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);

    

    /**

     保证buttonimageView在旋转之后保持原有的形状

     UIViewContentModeScaleToFill,  拉伸填充

     UIViewContentModeScaleAspectFit,  自适应

     UIViewContentModeScaleAspectFill,  自适应填充

     UIViewContentModeCenter,   保持原有的尺寸

     */

    

    headerButton.imageView.contentMode =UIViewContentModeCenter;

    

    // clipsToBounds YES; 超出父view的边界的部分将会被剪切掉

    headerButton.imageView.clipsToBounds =NO;

    

  3.  //为按钮添加点击事件

    [headerButton addTarget:self

                     action:@selector(didLClickButton:)

           forControlEvents:UIControlEventTouchUpInside];

    

4. //获取按钮图片和标题

UIImage *image = [btn backgroundImageForState:

UIControlStateHighlighted];//获取高亮状态下的图片;

 UIImage *img=btn.currentImage;//获取当前按钮的图片;

NSString *str =[btn titleForState:UIControlStateNormal];//获取正常状态下的按钮标题




        //取消失效状态的按钮图像调整

        self.adjustsImageWhenDisabled =NO;

        

        // 取消高亮状态的按钮图像调整

        self.adjustsImageWhenHighlighted =NO;


5.=======按钮中的点击事件:

UIControlEventTouchDown
单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。
UIControlEventTouchDownRepeat
多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。
UIControlEventTouchDragInside
当一次触摸在控件窗口内拖动时。
UIControlEventTouchDragOutside
当一次触摸在控件窗口之外拖动时。
UIControlEventTouchDragEnter
当一次触摸从控件窗口之外拖动到内部时。
UIControlEventTouchDragExit
当一次触摸从控件窗口内部拖动到外部时。
 
UIControlEventTouchUpInside
所有在控件之内触摸抬起事件。
UIControlEventTouchUpOutside
所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)。
UIControlEventTouchCancel
所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。
UIControlEventTouchChanged
当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。
UIControlEventEditingDidBegin
当文本控件中开始编辑时发送通知。
UIControlEventEditingChanged
当文本控件中的文本被改变时发送通知。
UIControlEventEditingDidEnd
当文本控件中编辑结束时发送通知。
UIControlEventEditingDidOnExit
当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。
UIControlEventAlltouchEvents
通知所有触摸事件。
UIControlEventAllEditingEvents
通知所有关于文本编辑的事件。
UIControlEventAllEvents

通知所有事件。

6.=============吧按钮放到最顶层

[self.view bringSubviewToFront:self.cameraBtn];


7。=================按钮标题自适应大小

*********1、确定LabelButton的字体大小,使其宽度自适应


UILabel *contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 120, 30)];

contentLabel.font = [UIFont systemFontOfSize:15];//-------->定义Font的大小

contentLabel.backgroundColor = [UIColor redColor];

contentLabel.text = @"我已知晓,且已阅读并同意以上条款";

[contentLabel sizeToFit];//-------->字体大小固定,宽度自适应

[self.View addSubview:contentLabel];

************2、确定LabelButton的宽度,使字体大小自适应


//无需自己设置字体大小

UILabel *contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 120, 30)];

contentLabel.backgroundColor = [UIColor redColor];

contentLabel.text = @"我已知晓,且已阅读并同意以上条款";

contentLabel.adjustsFontSizeToFitWidth = YES;//宽度固定,字体大小自适应

[self.View addSubview:contentLabel];

***********如果是Button的话,和上面一样,只有一点小小的区别:


[button.titleLabel  sizeToFit];

button.titleLabel.adjustsFontSizeToFitWidth = YES;




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值