UIButton头文件常见属性
1、属性
-
contentEdgeInsets
: default is UIEdgeInsetsZero.设置内容四边距,默认边距为0
@property(nonatomic) UIEdgeInsets contentEdgeInsets
-
titleEdgeInsets
: default is UIEdgeInsetsZero,文字内边距。默认边距为0.
@property(nonatomic) UIEdgeInsets titleEdgeInsets;
-
reversesTitleShadowWhenHighlighted
: default is NO. if YES, shadow reverses to shift between engrave and emboss appearance。默认状态是 NO,按钮高亮时,阴影出现在相反位置。
@property(nonatomic) BOOL reversesTitleShadowWhenHighlighted;
-
imageEdgeInsets
: default is UIEdgeInsetsZero。图片内边距,默认边距为0;
@property(nonatomic) UIEdgeInsets imageEdgeInsets;
-
adjustsImageWhenHighlighted
: default is YES. if YES, image is drawn darker when highlighted(pressed),当按钮高亮时是否调整图片。默认是 YES
@property(nonatomic) BOOL adjustsImageWhenHighlighted;
-
adjustsImageWhenDisabled
:default is YES. if YES, image is drawn lighter when disabled.默认是 yes,如果是 YES,图形绘制较轻时禁用
@property(nonatomic) BOOL adjustsImageWhenDisabled;
-
showsTouchWhenHighlighted
:default is NO. if YES, show a simple feedback (currently a glow) while highlighted。默认是 NO,如果是 YES,在高亮期间显示一个简单的反馈(当前发光).如果没有高亮状态,不做任何反馈。
@property(nonatomic) BOOL showsTouchWhenHighlighted;
-
tintColor
: The tintColor is inherited through the superview hierarchy. See UIView for more information.
这个属性是从 UIView 继承过来的,更多解释信息见 UIView.该属性不应用于UIButtonTypeCustom.,如果Custom 类型的按钮需要使用该属性,应该重写该属性的 set方法。
@property(null_resettable, nonatomic,strong) UIColor *tintColor
-
buttonType
:按钮样式属性,取值枚举类型
@property(nonatomic,readonly) UIButtonType buttonType;
-
currentTitle
:normal/highlighted/selected/disabled. can return nil。获取当前的文本。
@property(nullable, nonatomic,readonly,strong) NSString *currentTitle;
-
currentTitleColor
:normal/highlighted/selected/disabled. always returns non-nil. default is white(1,1)。当前文本颜色,默认白色。
@property(nonatomic,readonly,strong) UIColor *currentTitleColor;
-
currentTitleShadowColor
:normal/highlighted/selected/disabled.当前状态下的文本阴影颜色
@property(nullable, nonatomic,readonly,strong) UIColor *currentTitleShadowColor;
-
currentImage
:normal/highlighted/selected/disabled. can return nil。当前状态下的图片。
@property(nullable, nonatomic,readonly,strong) UIImage *currentImage;
-
currentBackgroundImage
:normal/highlighted/selected/disabled. can return nil。当前状态下的背景图片
@property(nullable, nonatomic,readonly,strong) UIImage *currentBackgroundImage;
-
currentAttributedTitle
:normal/highlighted/selected/disabled. can return nil。当前状态下的文本属性。
@property(nullable, nonatomic,readonly,strong) NSAttributedString *currentAttributedTitle;
-
titleLabel
:文本标签属性
@property(nullable, nonatomic,readonly,strong) UILabel *titleLabel;
-
imageView
:图片属性
@property(nullable, nonatomic,readonly,strong) UIImageView *imageView;
2、方法
由于按钮是有状态的,比如:未点击状态,高亮状态,选中状态,不可用状态等。所以按钮所提供的方法当中很多是根据按钮状态来设置的。
-
buttonWithType:
:类方法,创建按钮对象时,直接指定按钮类型。取值为枚举。
+ (instancetype)buttonWithType:(UIButtonType)buttonType;
-
setTitle:forState:
:default is nil. title is assumed to be single line。根据按钮状态设置文字。
- (void)setTitle:(nullable NSString *)title forState:(UIControlState)state;
-
setTitleColor:forState:
:default if nil. use opaque white。根据按钮状态设置文字颜色,默认白色。
- (void)setTitleColor:(nullable UIColor *)color forState:(UIControlState)state;
-
setTitleShadowColor: forState:
: default is nil. use 50% black。 设置文字阴影颜色,默认半透明黑色
- (void)setTitleShadowColor:(nullable UIColor *)color forState:(UIControlState)state ;
-
setImage: forState:
: default is nil. should be same size if different for different states。根据按钮状态设置图片。
- (void)setImage:(nullable UIImage *)image forState:(UIControlState)state;
-
setBackgroundImage: forState:
: 根据按钮状态设置背景图片。
- (void)setBackgroundImage:(nullable UIImage *)image forState:(UIControlState)state ;
-
setAttributedTitle: forState:
: default is nil. title is assumed to be single line.根据按钮状态设置文本属性内容(包括文字大小,颜色等)。假设文字是单行的。
- (void)setAttributedTitle:(nullable NSAttributedString *)title forState:(UIControlState)state;
-
titleForState: forState:
:these getters only take a single state value。根据按钮状态获取文本
- (nullable NSString *)titleForState:(UIControlState)state;
-
titleColorForState: forState:
: 根据按钮状态获取文本颜色
- (nullable UIColor *)titleColorForState:(UIControlState)state;
-
titleShadowColorForState: forState:
: 根据按钮状态获取文本阴影颜色
- (nullable UIColor *)titleShadowColorForState:(UIControlState)state;
-
imageForState: forState:
: 根据按钮状态获取图片。
- (nullable UIImage *)imageForState:(UIControlState)state;
-
backgroundImageForState: forState:
: 根据按钮状态获取背景图片。
- (nullable UIImage *)backgroundImageForState:(UIControlState)state;
-
attributedTitleForState: forState:
: 根据按钮状态获取文本属性。
- (nullable NSAttributedString *)attributedTitleForState:(UIControlState)state;
-
backgroundRectForBounds:
: 自定义按钮时,可以更改背景图片在按钮当中的位置。
- (CGRect)backgroundRectForBounds:(CGRect)bounds;
-
contentRectForBounds:
: 自定义按钮时,可以更改整个内容在按钮当中的位置。
- (CGRect)contentRectForBounds:(CGRect)bounds;
-
titleRectForContentRect:
: 自定义按钮时,可以更改Label在按钮当中的位置。
- (CGRect)titleRectForContentRect:(CGRect)contentRect;
-
imageRectForContentRect:
: 自定义按钮时,可以更改图片在按钮当中的位置。
- (CGRect)imageRectForContentRect:(CGRect)contentRect;