你的位置:首页 > 软件开发 > 操作系统 > iOS开发UIbutton常用属性方法

iOS开发UIbutton常用属性方法

发布时间:2017-08-17 16:00:09
对UIbutton的简单使用(基本上足够了),话不多说,直接看 UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom]; /* UIButtonType ...

对UIbutton的简单使用(基本上足够了),话不多说,直接看

    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];

    /*

     UIButtonTypeCustom = 0,           //自定义风格

     UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),  // 标准的系统按钮

     UIButtonTypeDetailDisclosure,   蓝色小箭头按钮,主要做详细说明用

     UIButtonTypeInfoLight,          蓝色感叹号

     UIButtonTypeInfoDark,           暗色感叹号

     UIButtonTypeContactAdd,         十字加字按钮

     UIButtonTypeRoundedRect = UIButtonTypeSystem,   // Deprecated, use UIButtonTypeSystem instead

     */

    myButton.frame = CGRectMake(100, 100, 100, 50);//button在view上面的位置

    [myButton setBackgroundColor:[UIColor brownColor]];//背景色

    [self.view addSubview:myButton];

    [myButton setImage:[UIImage imageNamed:@"buttonimage.gif"] forState:UIControlStateNormal];

//    [myButton setBackgroundImage:[UIImage imageNamed:@"buttonimage.gif"] forState:UIControlStateNormal];

    [myButton setTitle:@"不易" forState:UIControlStateNormal];

    // 设置图片的内边距

    /*

     UIControlStateNormal = 0, 常规状态显现

     UIControlStateHighlighted = 1 << 0, 高亮状态显现

     UIControlStateDisabled = 1 << 1, 禁用的状态才会显现

     UIControlStateSelected = 1 << 2, 选中状态

     UIControlStateApplication = 0x00FF0000, 当应用程序标志时

     UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管他

     */

    myButton.imageEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);

    

    // 设置文本的内边距

    myButton.titleEdgeInsets = UIEdgeInsetsMake(0, 50, 0, 0);

    /**

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

     UIViewContentModeScaleToFill,  拉伸填充

     UIViewContentModeScaleAspectFit,  自适应

     UIViewContentModeScaleAspectFill,  自适应填充

     UIViewContentModeCenter,   保持原有的尺寸

     */

    

    myButton.imageView.contentMode =UIViewContentModeCenter;

    /*

     * 默认情况下,当按钮高亮的情况下,图像的颜色会被画深一点,如果这下面的这个属性设置为no,

     * 那么可以去掉这个功能

     */

    myButton.adjustsImageWhenHighlighted = NO;

    /*跟上面的情况一样,默认情况下,当按钮禁用的时候,图像会被画得深一点,设置NO可以取消设置*/

    myButton.adjustsImageWhenDisabled = NO;

    /* 下面的这个属性设置为yes的状态下,按钮按下会发光*/

    myButton.showsTouchWhenHighlighted = YES;

    

    /* 给button添加事件,事件有很多种,我会单独开一篇博文介绍它们,下面这个时间的意思是

     按下按钮,并且手指离开屏幕的时候触发这个事件,跟web中的click事件一样。

     触发了这个事件以后,执行butClick:这个方法,addTarget:self 的意思是说,这个方法在本类中

     也可以传入其他类的指针*/

    [myButton addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];

    //取消按钮已经添加的所有事件:(这个比较重要,若添加了两个事件  两个事件都会被触发)

    [myButton removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];

    /*

     {

     UIControlEventTouchDown

     单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。

     UIControlEventTouchDownRepeat

     多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。

     UIControlEventTouchDragInside

     当一次触摸在控件窗口内拖动时。

     UIControlEventTouchDragOutside

     当一次触摸在控件窗口之外拖动时。

     UIControlEventTouchDragEnter

     当一次触摸从控件窗口之外拖动到内部时。

     UIControlEventTouchDragExit

     当一次触摸从控件窗口内部拖动到外部时。

     

     UIControlEventTouchUpInside

     所有在控件之内触摸抬起事件。

     UIControlEventTouchUpOutside

     所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)。

     UIControlEventTouchCancel

     所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。

     UIControlEventTouchChanged

     当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。

     UIControlEventEditingDidBegin

     当文本控件中开始编辑时发送通知。

     UIControlEventEditingChanged

     当文本控件中的文本被改变时发送通知。

     UIControlEventEditingDidEnd

     当文本控件中编辑结束时发送通知。

     UIControlEventEditingDidOnExit

     当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。

     UIControlEventAlltouchEvents

     通知所有触摸事件。

     UIControlEventAllEditingEvents

     通知所有关于文本编辑的事件。

     UIControlEventAllEvents

     通知所有事件。

     }

     */

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

    myButton.imageView.clipsToBounds =NO;

    //获取按钮图片和标题

    UIImage *image = [myButton backgroundImageForState:

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

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

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

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

    myButton.adjustsImageWhenDisabled =NO;

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

    myButton.adjustsImageWhenHighlighted =NO;

    

    //=================按钮标题自适应大小

    

    //*********1、确定Label或Button的字体大小,使其宽度自适应

    

    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、确定Label或Button的宽度,使字体大小自适应

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

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

    contentLabel1.backgroundColor = [UIColor redColor];

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

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

    [self.view addSubview:contentLabel1];

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

    [myButton.titleLabel  sizeToFit];

    myButton.titleLabel.adjustsFontSizeToFitWidth = YES;

    

    //设置边框颜色和圆角

    [myButton.layer setMasksToBounds:YES];

    [myButton.layer setCornerRadius:10.0]; //设置矩形四个圆角半径

    //边框宽度

    [myButton.layer setBorderWidth:1.0];

    //设置边框颜色有两种方法:第一种如下:

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 0, 0, 0, 1 });

    [myButton.layer setBorderColor:colorref];//边框颜色

    //第二种方法如下:

    //_testButton.layer.borderColor=[UIColor grayColor].CGColor;

 

方便自己使用,也希望能方便他人

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:iOS开发UIbutton常用属性方法

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。