码迷,mamicode.com
首页 > 其他好文 > 详细

UIButton

时间:2015-12-23 14:13:03      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:

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 位置,默认是图片在左,文字在右。但是在此,我想让图片在上,文字在下。

    • 默认样式
      技术分享
    • 调整后
      技术分享
    • 代码
      1.
      2.button.titleEdgeInsets = UIEdgeInsetsMake(100, -100, 0, 0);
      3.button.imageEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
      4.
 

UIButton

标签:

原文地址:http://www.cnblogs.com/buakaw/p/5069716.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!