iOS控件--UIButton

- (void)viewDidLoad {
    [super viewDidLoad];
    
    /*  系统自带的一些button类型
     *  UIButtonTypeCustom = 0,        自定义风格
     *  UIButtonTypeRoundedRect,       圆角矩形
     *  UIButtonTypeDetailDisclosure,  蓝色小箭头按钮,主要做详细说明用
     *  UIButtonTypeInfoLight,         亮色感叹号
     *  UIButtonTypeInfoDark,          暗色感叹号
     *  UIButtonTypeContactAdd,        十字加号按钮
     */
    //按钮初始化
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    //btn的背景颜色
    btn.backgroundColor = [UIColor yellowColor];
    
    //btn在view的位置
    btn.frame = CGRectMake(100, 100, 100, 100);
    
    //按钮的layer属性
    //打开边缘属性
    btn.layer.masksToBounds = YES;
    //设置圆角半径
    btn.layer.cornerRadius = 30;
    //设置边缘线颜色
    btn.layer.borderColor = [[UIColor orangeColor] CGColor];
    //设置边缘线宽
    btn.layer.borderWidth = 3;
    
    /*设置btn的title
     // UIControlStateNormal = 0,               常规状态显现
     // UIControlStateHighlighted = 1 << 0,     高亮状态显现
     // UIControlStateDisabled = 1 << 1,        禁用的状态才会显现
     // UIControlStateSelected = 1 << 2,        选中状态
     // UIControlStateApplication = 0x00FF0000, 当应用程序标志时
     // UIControlStateReserved = 0xFF000000     为内部框架预留,可以不管他
     */
    [btn setTitle:@"点我" forState:UIControlStateNormal];
    [btn setTitle:@"好疼" forState:UIControlStateHighlighted];
    
    //按钮字体颜色
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    
    //按钮字体大小(titleLabel.text为title)
    //titleLabel具有所有的label属性
    btn.titleLabel.font = [UIFont systemFontOfSize:40];
    
    //苹果官方文档的解释大体上可以理解为,这个属性设置的是内边距,更通俗点说,是规定,Button向内的多少区域其它控件是不能进入的
    //{CGFloat top, left, bottom, right;}
    btn.contentEdgeInsets = UIEdgeInsetsMake(20, 0, 0, 0);
    
    //按钮添加点击事件
    /*
    *  UIControlEventTouchDown,         单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。
    *  UIControlEventTouchDownRepeat,   多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。
    *  UIControlEventTouchDragInside,   当一次触摸在控件窗口内拖动时。
    *  UIControlEventTouchDragOutside,  当一次触摸在控件窗口之外拖动时。
    *  UIControlEventTouchDragEnter,    当一次触摸从控件窗口之外拖动到内部时。
    *  UIControlEventTouchDragExit,     当一次触摸从控件窗口内部拖动到外部时。
     
    *  UIControlEventTouchUpInside,     所有在控件之内触摸抬起事件。
    *  UIControlEventTouchUpOutside,    所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)。
    *  UIControlEventTouchCancel,       所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。
    *  UIControlEventTouchChanged,      当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。
                                        你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。
    *  UIControlEventEditingDidBegin,   当文本控件中开始编辑时发送通知。
    *  UIControlEventEditingChanged,    当文本控件中的文本被改变时发送通知。
    *  UIControlEventEditingDidEnd,     当文本控件中编辑结束时发送通知。
    *  UIControlEventEditingDidOnExit,  当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。
    *  UIControlEventAlltouchEvents,    通知所有触摸事件。
    *  UIControlEventAllEditingEvents,  通知所有关于文本编辑的事件。
    *  UIControlEventAllEvents,         通知所有事件。
     */
    [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.view addSubview:btn];
}

- (void)click {
    NSLog(@"按钮被点击了");
}

titleEdgeInsets 和 imageEdgeInsets

UIButton内有两个控件titleLabel和imageView,可以用来显示一个文本和图片,这里的图片区别于背景图片。给UIButton设置了title和image后,它们会图片在左边,文本在图片右边显示。它们两个做为一个整体依赖于button的contentHorizontalAlignment居左居右或居中显示。

    1.当button.width < image.width时,只显示被压缩后的图片,图片是按fillXY的方式压缩。

     2.当button.width > image.width,且 button.width < (image.width + text.width)时,图片正常显示,文本被压缩。

     3.当button.width > (image.width + text.width),两者并列默认居中显示,可通过button的属性contentHorizontalAlignment改变对齐方式。

     4.想两改变两个子控件的显示位置,可以分别通过setTitleEdgeInsets和setImageEdgeInsets来实现。需要注意的是,对titleLabel和imageView设置偏移,是针对它当前的位置起作用的,并不是针对它距离button边框的距离的。我测试下来,当button的contentHorizontalAlignment为居中时,偏移的距离和实际传的值有些偏差,没有发现规律,看不到源码也没在研究,但把button的contentHorizontalAlignment设为居左时,contentVerticalAlignment设为居上时,可以很方便的通过EdgeInsets改变两个子控件的位置

adjustsImageWhenDisabled

一个布尔值,决定是否形象的变化时,该按钮被禁用。

@属性(非原子)BOOL adjustsImageWhenDisabled讨论,如果是的话,图像绘制较深时,按钮被禁用。默认值是YES。

adjustsImageWhenHighlighted

一个布尔值,决定是否按钮时,突出显示图像的变化。

@属性(非原子)BOOL adjustsImageWhenHighlighted讨论,如果是,绘制图像较轻的按钮时,突出显示。默认值是YES。

button一些其他属性

@property(nullable, nonatomic,readonly,strong) NSString *currentTitle;             // normal/highlighted/selected/disabled. can return nil
@property(nonatomic,readonly,strong) UIColor  *currentTitleColor;        // normal/highlighted/selected/disabled. always returns non-nil. default is white(1,1)
@property(nullable, nonatomic,readonly,strong) UIColor  *currentTitleShadowColor;  // normal/highlighted/selected/disabled.
@property(nullable, nonatomic,readonly,strong) UIImage  *currentImage;             // normal/highlighted/selected/disabled. can return nil
@property(nullable, nonatomic,readonly,strong) UIImage  *currentBackgroundImage;   // normal/highlighted/selected/disabled. can return nil
@property(nullable, nonatomic,readonly,strong) NSAttributedString *currentAttributedTitle NS_AVAILABLE_IOS(6_0);  // normal/highlighted/selected/disabled. can return nil

// return title and image views. will always create them if necessary. always returns nil for system buttons
@property(nullable, nonatomic,readonly,strong) UILabel     *titleLabel NS_AVAILABLE_IOS(3_0);
@property(nullable, nonatomic,readonly,strong) UIImageView *imageView  NS_AVAILABLE_IOS(3_0);


转载于:https://my.oschina.net/neilWXD/blog/650952

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值