简单几行代码实现UIButton 上图下文
2017-03-24 本文已影响70人
CocoaJason
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define HeightIn667(Height) (Height * ScreenHeight / 667)
#define WidthIn375(Width) (Width * ScreenWidth / 375)
@interface CustomBtn : UIButton
-(void)setTitle:(NSString *)title
image:(UIImage *)image;
@end
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
CGRect titleRect = CGRectMake(0, CGRectGetHeight(self.bounds)/3 * 2, CGRectGetWidth(self.bounds),CGRectGetHeight(self.bounds)/3);
return titleRect;
}
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
CGFloat width = CGRectGetWidth(self.bounds);
CGRect imageRect = CGRectMake(0, 0, width, CGRectGetHeight(self.bounds)/3 * 2);
return imageRect;
}
-(void)setTitle:(NSString *)title
image:(UIImage *)image;
{
[self setTitle:title forState:UIControlStateNormal];
[self setImage:image forState:UIControlStateNormal];
}
CustomBtn *btn = [[CustomBtn alloc]initWithFrame:CGRectMake(20, 100, 80, 120)];
[self.view addSubview:btn];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setTitle:@"测试一下" image:[UIImage imageNamed:@"placeholderHeadIcon"]];