UIButton 内部介绍

**
     设置内容距btn边框距离
     **/
    btn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    
    /** 将label和imageView看成一个整体
     比如将imageview放到label后面 则需要计算 imageview与label相对的位置 和距离另外一个边的距离
     通俗的说 默认情况下的 imageview在左 titlelabel在右
     imageEdgeInsets left对比位置未 按钮左边框  right 对比与他相邻的label
     titlelabelEdgeInsets 与 image相同
     下面代码为  左右交换image与label
    **/
    
    [btn setImageEdgeInsets:UIEdgeInsetsMake(0,btn.titleLabel.frame.size.width, 0,-btn.titleLabel.frame.size.width-btn.imageView.frame.size.width)];
    
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0,CGRectGetWidth(btn.imageView.frame))];

 

这个方法计算起来比较麻烦  不如直接自定义button 来 重写layoutSubviews方法 直接设置 image lable frame 来达到效果

注:[super layoutSubviews]


    /**
     reversesTitleShadowWhenHighlighted 设置标题高亮时 阴影是否突出显示
     adjustsImageWhenHighlighted 默认是肯定的。如果是的话,图像被高亮显示时(按下)
     adjustsImageWhenDisabled 默认是肯定的。如果是,图像被禁用时禁用
     showsTouchWhenHighlighted 默认情况下是没有。如果是,显示一个简单的反馈(目前为辉光),而高亮显示
     **/
    
        btn.reversesTitleShadowWhenHighlighted = NO;
        btn.showsTouchWhenHighlighted = YES;


    //设置阴影颜色
    [btn setTitleShadowColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn setTitleShadowColor:[UIColor greenColor] forState:UIControlStateHighlighted];


    //attributedString标题设置
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:[UIColor redColor] forKey:NSBackgroundColorAttributeName];
    NSAttributedString * aStr = [[NSAttributedString alloc]initWithString:@"33333" attributes:dic];
    [btn setAttributedTitle:aStr forState:UIControlStateNormal];


    /**
     获取相应状态的属性值
     - (nullable UIColor *)titleColorForState:(UIControlState)state
     - (nullable UIColor *)titleShadowColorForState:(UIControlState)state
     - (nullable UIImage *)imageForState:(UIControlState)state
     - (nullable UIImage *)backgroundImageForState:(UIControlState)state
     - (nullable NSAttributedString *)attributedTitleForState:(UIControlState)state
     **/
    
/*
 在子类化的时候你可以重载下面这些方法,这些方法返回CGRect结构,指明了按钮每一组成部分的边界。
 
 注意:不要直接调用这些方法, 这些方法是你写给系统调用的。
 - (CGRect)backgroundRectForBounds:(CGRect)bounds  制定背景边框
 - (CGRect)contentRectForBounds:(CGRect)bounds 制定内容边界
 - (CGRect)titleRectForContentRect:(CGRect)contentRect  文字标题边界
 - (CGRect)imageRectForContentRect:(CGRect)contentRect 按钮图像边界
 */

posted on 2016-03-17 17:55  GG.Bong  阅读(243)  评论(0编辑  收藏  举报