文本混排,label显示图片

刚发现这个类好用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];

你可能感兴趣的:(文本混排,label显示图片)