[iOS]UIButton源码分析

首先说为什么要写这个,因为上个月面试问到了,当时看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阴影偏移量





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/*========================================================================*/ // 事件名称: readme in uo_imflatbutton * (pdm) inherited from commandbutton /*------------------------------------------------------------------------*/ // 声明:本按钮原型为“扁平按钮”,扁平按钮功能部分归原始作者所有 /*-------------------------------------------------------------------------*/ // 描述: 本对象为自定义按钮对象,修改自扁平按钮 // 主要功能为:扁平按钮形式 〔包括原扁平按钮的所有功能〕 // 渐进色状态 〔具有渐近色的按钮〕 // 正常按钮状态 〔修饰了的原始PB按钮〕 // 新增两种XP风格按钮 〔焦点状态颜色不同〕 // 图片按钮状态〔可用pb自己的图片〕 // 椭圆形按钮状态 〔在以上几种按钮形态下把按钮转换为椭圆形〕 // 提示信息显示 〔显示按钮的提示信息〕 // 用户可自定义各种属性,包括各种颜色、图片、状态等 // 具体属性请看按钮的属性值,注:按钮的属性为中文属性 // // 增加PB自带图片锁定功能〔如果图片为PB自带图片,只要选择按钮的PB自带图片 // 属性即可锁定图片大小为原始尺寸〕 // 增加了所有状态的属性开关,包括: // 可视 可用 默认 取消 // 椭圆形状〔确定按钮是否为椭圆形状〕 // 边框 〔确定按钮是否有边框〕 // 凸起效果 〔确定按钮是否有凸起效果〕 // 凸起效果渐进色背景 〔确定按钮是否有凸起效果渐进色背景〕 // 渐进色背景 〔确定按钮是否有渐进色背景〕 // 水平渐进 〔确定按钮渐进色背景为水平渐进还是竖直渐近〕 // PB自带图片 〔锁定PB自带图片时的图片大小〕 // 提示信息显示 〔是否显示提示信息〕 // 按钮背景颜色设置 〔设置按钮的背景颜色〕 /*------------------------------------------------------------------------*/ // 说明:只包括一个按钮对象 /*------------------------------------------------------------------------*/ // 作者: pcm 日期: 2003-11-11

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值