不知道大家有木有碰到下面这种图标,其实这种在iOS应用上是经常看到的,拿到这种样式的东西怎么做呢?封装,UIImageView+UILabel+UILabel加到UIButton上就可以了,但是这个局限性太大了,很多时候根本达不到那种效果,比如点击阴影,使用的流畅性!这时候我们就得想办法了,而最直接有效的办法就莫过于使用UIButton上自带的imageview&titlelabel做文章了。详情见代码,已经很详细!我的图片是56*56 @2x的,如果大小不同,再稍微调整,为了上传方便,我就用本地图片代替url地址!
原理自行百度!
1 #define RGBACOLOR(r,g,b,a) \ 2 [UIColor colorWithRed:r/255.f green:g/255.f blue:b/255.f alpha:a] 3 4 #import "UIButton+Service.h" 5 @implementation UIButton (Service) 6 - (void)MyButtonWithFrame:(CGRect)frame 7 textString:(NSString *)textString 8 subString:(NSString *)subString 9 imageUrl:(NSString *)imageUrl 10 11 { 12 13 self.backgroundColor = [UIColor whiteColor]; 14 self.frame = frame; 15 [self setImage:[UIImage imageNamed:imageUrl] forState:UIControlStateNormal]; 16 [self setImage:[UIImage imageNamed:imageUrl] forState:UIControlStateHighlighted]; 17 18 [self setTitle:textString forState:UIControlStateNormal]; 19 [self setTitleColor:RGBACOLOR(59, 59, 59, 1.0) forState:UIControlStateNormal]; 20 self.titleLabel.font = [UIFont fontWithName:@"Arial" size:13]; 21 22 23 self.titleLabel.numberOfLines = 0; 24 self.titleLabel.textAlignment = NSTextAlignmentCenter; 25 26 //行间距离 27 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; 28 paragraphStyle.alignment = NSTextAlignmentCenter; 29 paragraphStyle.lineSpacing = 6.0; 30 NSDictionary *ats = @{ 31 NSParagraphStyleAttributeName : paragraphStyle, 32 }; 33 34 //文字分行 并设置字体颜色,大小 35 NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@\n%@",textString,subString] attributes:ats]; 36 37 [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:15] range:NSMakeRange(0, textString.length)]; 38 [str addAttribute:NSForegroundColorAttributeName value:RGBACOLOR(59, 59, 59, 1.0) range:NSMakeRange(0, textString.length)]; 39 [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:10] range:NSMakeRange(str.length - subString.length, subString.length)]; 40 [str addAttribute:NSForegroundColorAttributeName value:RGBACOLOR(0xA0, 0xA0, 0xA0, 1.0) range:NSMakeRange(str.length - subString.length, subString.length)]; 41 [self setAttributedTitle:str forState:UIControlStateNormal]; 42 43 //调整图片和字体的位置 44 CGSize titleSize = [textString boundingRectWithSize:CGSizeMake(0, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Arial" size:15]} context:nil].size; 45 CGSize titleSizea = [subString boundingRectWithSize:CGSizeMake(0, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Arial" size:10]} context:nil].size; 46 CGFloat t = (titleSize.width > titleSizea.width)?titleSize.width:titleSizea.width; 47 [self setImageEdgeInsets:UIEdgeInsetsMake(-32.0,0.0,0,-t)]; 48 [self setTitleEdgeInsets:UIEdgeInsetsMake(60.0,-28,0.0,0.0)]; 49 } 50 51 @end
viewController.m调用
1 #import "ViewController.h" 2 #import "UIButton+Service.h" 3 @interface ViewController () 4 5 @end 6 7 @implementation ViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 self.view.backgroundColor = [UIColor whiteColor]; 12 13 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 14 button.layer.borderWidth = 1.0; 15 button.layer.borderColor = [UIColor redColor].CGColor; 16 [button MyButtonWithFrame:CGRectMake(100, 200, 100, 100) textString:@"这是主题" subString:@"这是描述语句" imageUrl:@"2"]; 17 [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; 18 [self.view addSubview:button]; 19 } 20 21 22 #pragma mark -- 自定义点击事件 23 - (void)buttonPressed:(UIButton *)sender{ 24 NSLog(@"ss"); 25 } 26 27 @end