[iOS]UIButton源码分析

原创 2015年11月21日 16:01:12

首先说为什么要写这个,因为上个月面试问到了,当时看IOS也才一两个月,水平的确是不行大哭,现在有时间把这块补一补。


下面都是UIButton自己的方法和属性,不包括从UIControl,UIView,UIResponder和NSObject继承过来的。


typedef NS_ENUM(NSInteger, UIButtonType) {

    UIButtonTypeCustom = 0,                        // no button type

    UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0), // standard system button


    UIButtonTypeDetailDisclosure,

    UIButtonTypeInfoLight,

    UIButtonTypeInfoDark,

    UIButtonTypeContactAdd,

    

    UIButtonTypeRoundedRect = UIButtonTypeSystem,  // Deprecated, use UIButtonTypeSystem instead

};



初始化的时候可以定义的button类型,一般自定义Button用UIbuttonTypeCustom就可以了。


+ (instancetype)buttonWithType:(UIButtonType)buttonType;


类方法,用上边的定义新建一个button。


@property(nonatomic)          UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR; // default is UIEdgeInsetsZero

UIEdgeInsets是一个结构体,表示内容与边界的间隙。

@property(nonatomic)          UIEdgeInsets titleEdgeInsets;                // default is UIEdgeInsetsZero

title与边界的间隙。

@property(nonatomic)          BOOL         reversesTitleShadowWhenHighlighted; // default is NO. if YES, shadow reverses to shift between engrave and emboss appearance

确定高亮时title的阴影。

@property(nonatomic)          UIEdgeInsets imageEdgeInsets;                // default is UIEdgeInsetsZero

图片与button的间隙

@property(nonatomic)          BOOL         adjustsImageWhenHighlighted;    // default is YES. if YES, image is drawn darker when highlighted(pressed)

高亮时图片是否变暗

@property(nonatomic)          BOOL         adjustsImageWhenDisabled;       // default is YES. if YES, image is drawn lighter when disabled

当disabled时图片是否变亮

@property(nonatomic)          BOOL         showsTouchWhenHighlighted __TVOS_PROHIBITED;      // default is NO. if YES, show a simple feedback (currently a glow) while highlighted

高亮动作

@property(null_resettable, nonatomic,strong)   UIColor     *tintColor NS_AVAILABLE_IOS(5_0); // The tintColor is inherited through the superview hierarchy. See UIView for more information.

文字颜色

@property(nonatomic,readonly) UIButtonType buttonType;

button类型





state继承自UIControl


- (void)setTitle:(nullable NSString *)title forState:(UIControlState)state;                     // default is nil. title is assumed to be single line

设置文字

- (void)setTitleColor:(nullable UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default if nil. use opaque white

title颜色

- (void)setTitleShadowColor:(nullable UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default is nil. use 50% black

title阴影颜色

- (void)setImage:(nullable UIImage *)image forState:(UIControlState)state;                      // default is nil. should be same size if different for different states

image设置

- (void)setBackgroundImage:(nullable UIImage *)image forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default is nil

北京图片

- (void)setAttributedTitle:(nullable NSAttributedString *)title forState:(UIControlState)state NS_AVAILABLE_IOS(6_0); // default is nil. title is assumed to be single line

设置AttributeTitle,具体可查看AttributeString。




- (nullable NSString *)titleForState:(UIControlState)state;          // these getters only take a single state value

- (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 NS_AVAILABLE_IOS(6_0);



返回特定状态下的title或image等


@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



nullable表示可以返回空值



获取当前状态下的各项属性,都是只读


@property(nullable, nonatomic,readonly,strong) UILabel     *titleLabel NS_AVAILABLE_IOS(3_0);

@property(nullable, nonatomic,readonly,strong) UIImageView *imageView  NS_AVAILABLE_IOS(3_0);


返回上面说的两个属性



- (CGRect)backgroundRectForBounds:(CGRect)bounds;

- (CGRect)contentRectForBounds:(CGRect)bounds;

- (CGRect)titleRectForContentRect:(CGRect)contentRect;

- (CGRect)imageRectForContentRect:(CGRect)contentRect;



边界范围内所要属性的边界



@property(nonatomic,strong) UIFont         *font              NS_DEPRECATED_IOS(2_0, 3_0) __TVOS_PROHIBITED;

@property(nonatomic)        NSLineBreakMode lineBreakMode     NS_DEPRECATED_IOS(2_0, 3_0) __TVOS_PROHIBITED;

@property(nonatomic)        CGSize          titleShadowOffset NS_DEPRECATED_IOS(2_0, 3_0) __TVOS_PROHIBITED;



字面意思,设置文字,换行模式,title阴影偏移量





版权声明:本文为博主原创文章,未经博主允许不得转载。

iOS 验证码 小技术之 UIButton NSTimer 计时器

iOS 验证码 小技术之 NSTimer 计时器 UIButton

ios uibutton开发

  • 2015年07月01日 10:14
  • 96KB
  • 下载

[iOS]EasyJsWebView 源码分析

原文:http://ios.jobbole.com/85273/ 最近在做hybrid相关的工作,项目中用到了EasyJsWebView,代码量不大,一直想分析一下它的具体实现,抽空...

IOS_UI_UIButton

  • 2015年05月24日 14:44
  • 422B
  • 下载

iOS Block源码分析系列(二)————局部变量的截获以及__block的作用和理解

1.介绍Block如何捕获局部变量 2.__block的内部原理和作用 总得来说: 所谓的Block捕获自动变量,意味着在iBlock语法额时候,Block语法表达式所用的自动变量值被保存到__mai...

仿iOS 7后台侧边菜单源码分析和总结

转自:http://www.2cto.com/kf/201403/283981.html 前言 首先看看效果图: 参考的源码来源于:TWTSideMe...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[iOS]UIButton源码分析
举报原因:
原因补充:

(最多只允许输入30个字)