标签:
UIButton的详细介绍:
一、按钮具有的属性:
@property(nonatomic,readonly) UIButtonType buttonType; //按钮类型
@property(nonatomic,readonly,retain) NSString *currentTitle; //按钮当前文字
@property(nonatomic,readonly,retain) UIColor *currentTitleColor; //按钮当前文字颜色
@property(nonatomic,readonly,retain) UIColor *currentTitleShadowColor; //按钮文字当前阴影颜色
@property(nonatomic,readonly,retain) UIImage *currentImage; //按钮当前前景图片
@property(nonatomic,readonly,retain) UIImage *currentBackgroundImage; //按钮当前背景图片
@property(nonatomic,readonly,retain) NSAttributedString *currentAttributedTitle //按钮文字当前属性
@property(nonatomic,readonly,retain) UILabel *titleLabel //按钮标签
@property(nonatomic,readonly,retain) UIImageView *imageView //按钮视图
@property(nonatomic) UIControlContentVerticalAlignment contentVerticalAlignment; //按钮垂直放置方式
@property(nonatomic) UIControlContentHorizontalAlignment contentHorizontalAlignment; //按钮水平放置方式
@property(nonatomic,readonly) UIControlState //按钮状态类型
二、设置按钮的属性值
- (void)setTitle:(NSString *)title forState:(UIControlState)state; //设置按钮文字内容
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state //设置按钮文字颜色
- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state //设置按钮文字阴影颜色
- (void)setImage:(UIImage *)image forState:(UIControlState)state; //设置按钮前景图片
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state //设置按钮背景图片
- (void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)state //设置按钮文字属性
三、按钮的状态类型
按钮类型UIControlState:
UIControlStateNormal //正常类型
UIControlStateHighlighted //高亮类型
UIControlStateDisabled //禁用类型
UIControlStateSelected //选中类型
UIControlStateApplication //当应用程序标识使用时
UIControlStateReserved //为框架预留的
四、获取按钮的属性
- (NSString *)titleForState:(UIControlState)state; //获取按钮文字
- (UIColor *)titleColorForState:(UIControlState)state; //获取按钮文字颜色
- (UIColor *)titleShadowColorForState:(UIControlState)state; //获取按钮文字阴影颜色
- (UIImage *)imageForState:(UIControlState)state; //获取按钮前景图片
- (UIImage *)backgroundImageForState:(UIControlState)state; //获取按钮背景图片
- (NSAttributedString *)attributedTitleForState:(UIControlState)state; //获取按钮文字属性
五、按钮文字放置方式
垂直放置:
UIControlContentVerticalAlignmentCenter //居中
UIControlContentVerticalAlignmentTop //置顶
UIControlContentVerticalAlignmentBottom //置底
UIControlContentVerticalAlignmentFill //填充
水平放置:
UIControlContentHorizontalAlignmentCenter //居中
UIControlContentHorizontalAlignmentLeft //居左
UIControlContentHorizontalAlignmentRight //居右
UIControlContentHorizontalAlignmentFill //填充
说明:
(1) 由于按钮有状态类型之分,所以,在给按钮添加文字时,使用button.TitleLabel.Text = @“按钮”这种赋值方式是无效的,在视图中不会显示出来,应该使用[button setTitle:(NSString *)title forState:(UIControlState)
state]这种方式才是有效地。同样设置文字的颜色也是如此:
设置UIButton上字体的颜色设置UIButton上字体的颜色,不是用:
[btn.titleLabel setTextColor:[UIColorblackColor]];
btn.titleLabel.textColor=[UIColor redColor];
而是用:
[btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
(2)获取按钮的文字,应该使用[button currentTitle],如果使用button.titleLabel.Text,其结果并不是你设置的文字内容。同样获取文字的颜色也是如此.[button currentTitleColor]
(3)设置按钮上的字体的大小
[button setFont: [UIFont systemFontSize: 14.0]]; //这种可以用来设置字体的大小,但是可能会在 将来的SDK版本中去除改方法
button.titleLabel.font = [UIFont fontWithName:(NSString*)fontName size:14.0]; //应该使用
或者
button.TitleLabel.font = [UIFont systemFontOfSize: 14.0]; //应该使用
(4) 有些时候我们想让UIButton的title居左对齐
button.textLabel.textAlignment = UITextAlignmentLeft //是没有作用的,我们需要设置
button.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft; //显示居左
但是问题又出来,文字会紧贴到做边框,我们可以设置使文字距离左边框保持10个像素的距离。
button.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
标签:
原文地址:http://www.cnblogs.com/XYQ-208910/p/4774082.html