1.button改变文字的大小
titleLabel是readonly属性 @property中readonly表示不允许修改对象的指针地址,但是可以修改对象的属性:
button.titleLabel.font= [UIFont systemFontOfSize:14.0];
2.button的对齐方式(titleLabel和imageView默认是居中显示的)
设置的对齐方式(左对齐)
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
设置titleLabel的内边距(向右移动20)
button.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
设置imageView的内边距(向右移动20)
button.imageEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
3.button的高亮操作
button默认在选中的时候会调整图片,有时候需求是在选中的时候不要改变图片,解决办法:
1>.设置高亮时不调整图片:
btn.adjustsImageWhenHighlighted = NO;
2>.自定义类:
在选中按钮的时候系统会做一些复杂的操作,会使button不会立即切换到对应状态的图片,复写以下方法:
-(void)setHighlighted:(BOOL)highlighted{
// [super setHighlighted:highlighted];(取消复杂操作)
}
4.修改button内部titleLabel和imageView的frame
方式1:
自定义button,实现
- (CGRect)imageRectForContentRect:(CGRect)contentRect;
和- (CGRect)titleRectForContentRect:(CGRect)contentRect;
方法重新计算两者的frame,其中contentRect是button的frame方式2:
自定义button,实现
- (void)layoutSubviews
方法重新计算两者的frame,并且比第一种方式简单易用