UIButton

UIButton

首先需要说明UIButton继承自UIControl,UIControl继承自UIView

  • 常用属性

    • 调整titleLabel和imageView的相对位置

      1.
      2.@property (nonatomic) UIEdgeInsets titleEdgeInsets;
      3.@property (nonatomic) UIEdgeInsets imageEdgeInsets;
    • titleLabel和imageView

      1.@property (nonatomic,readonly,strong) UILabel *titleLabel;
      2.@property (nonatomic,readonly,strong) UIImageView *imageView;
    • 需要注意的是:修改title的字体大小

      1.button.titleLabel.font = [UIFont systemFontOfSize:14.0f];

  • 继承自UIControl的属性

    1.@property(nonatomic,getter=isEnabled) BOOL enabled;                                  // default is YES. if NO, ignores touch events and subclasses may draw differently
    2.@property(nonatomic,getter=isSelected) BOOL selected;
    3.// default is NO.
    4.@property(nonatomic,getter=isHighlighted) BOOL highlighted;
    5.// default is NO.
    6.@property(nonatomic,readonly) UIControlState state;

  • 常用方法

    • 设置title
      - (void)setTitle:(nullable NSString *)title forState:(UIControlState)state;
    • 设置title颜色
      - (void)setTitleColor:(nullable UIColor *)color forState:(UIControlState)state
    • 设置image
      - (void)setImage:(nullable UIImage *)image forState:(UIControlState)state;
    • 设置背景图
      - (void)setBackgroundImage:(nullable UIImage *)image forState:(UIControlState)state
    • 设置富文本title
      - (void)setAttributedTitle:(nullable NSAttributedString *)title forState:(UIControlState)state
    • 子类调整imageView和titleLabel的位置可重写下面方法
      1.
      2.- (CGRect)titleRectForContentRect:(CGRect)contentRect;
      3.- (CGRect)imageRectForContentRect:(CGRect)contentRect;
      4.

  • 继承自UIControl的方法
    • 添加监听
      - (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
    • 移除监听
      - (void)removeTarget:(nullable id)target action:(nullable SEL)action forControlEvents:(UIControlEvents)controlEvents;

  • 示例:展示调整titleLabel 和 imageView 位置,默认是图片在左,文字在右。但是在此,我想让图片在上,文字在下。

    • 默认样式
      Alt text
    • 调整后
      Alt text
    • 代码
      1.
      2.button.titleEdgeInsets = UIEdgeInsetsMake(100, -100, 0, 0);
      3.button.imageEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
      4.
 
posted @ 2015-12-23 13:41  Emerys  阅读(201)  评论(0编辑  收藏  举报