刚发现这个类好用NSTextAttachment图文混排,不用再去麻烦计算图片的位置去移动,简单实现button,label的图文混排的效果,记录一下。
UIButton*zongheButton =[[UIButtonalloc]initWithFrame:CGRectMake(30,50,100,60)];
[self.viewaddSubview: zongheButton];
NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:@"综合排序"];
NSTextAttachment *attch = [[NSTextAttachment alloc] init];
attch.image= [UIImageimageNamed:@"offLine_phone"];
attch.bounds=CGRectMake(5,0,20,15);
NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
[attriappendAttributedString:string];
[attriaddAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, attri.length)];
[zongheButtonsetAttributedTitle:attri forState:UIControlStateNormal];
网友评论