UIButton - 常用方法总结

注 : 文章不断更新,转载文章请加上作者



// 设置按钮的文字  title : 需要设置的文字   state : 该文字显示的状态
- (void)setTitle:(NSString *)title forState:(UIControlState)state; 

// 设置按钮的文字颜色   color : 设置颜色  state : 该文字颜色显示的状态
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;

// 设置按钮内部的小图片 state : 该图片显示的状态
- (void)setImage:(UIImage *)image forState:(UIControlState)state; 

// 获取按钮上传入的状态的文本   state : 需要获取文本的状态
- (NSString *)titleForState:(UIControlState)state; 

// 设置按钮的背景图片 image: 图片  state 该图片显示的状态
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state

// 获取按钮内部的小图片
- (UIImage *)imageForState:(UIControlState)state;

//获取按钮内部的背景图片
- (UIImage *)backgroundImageForState:(UIControlState)state;

//获取按钮的文字颜色
- (UIColor *)titleColorForState:(UIControlState)state;


// 按钮中,包括了一个 UILabel 和一个 UIImageView 的子控件
// 所以设置按钮的文字字体,就获取UILabel,去设置Font属性
// @property(nonatomic,readonly,retain) UILabel *titleLabel
// @property(nonatomic,readonly,retain) UIImageView *imageView

UIButton *btnTest = [[UIButton alloc] init];
btnTest.titleLabel.font = [UIFont systemFontOfSize:13];




UIButton有很多状态,它提供了一些便捷属性, 可以直接获取当前状态下得文字, 文字颜色 , 图片等.

@property (nonatomic, readonly, retain) NSString *currenTitle; // 获取文字
@property (nonatomic, readonly, retain) UIColor *currenTitleColor; // 获取文字颜色
@property (nonatomic, readonly, retain) UIImage *currenImage// 获取按钮内部小图片
@property (nonatomic, readonly, retain) UIImage *currenBackGroundImagel; // 获取按钮背景图片







猜你喜欢

转载自cwlong.iteye.com/blog/2227601