iOS UILable 文字添加图片 (文字前面,中间,后面添加图)


1,实例化一个UILable

2,

 // 创建一个富文本

    NSMutableAttributedString *attri =     [[NSMutableAttributedString alloc] initWithString:@"超时罚款24元/小时"];

    NSTextAttachment *attch = [[NSTextAttachment alloc] init];

    // 表情图片

    attch.image = [UIImage imageNamed:@"parking_caveat_normal"];

    // 设置图片大小

    attch.bounds = CGRectMake(0, 0, 30/2, 27/2);

    // 创建带有图片的富文本

    NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];

[attri appendAttributedString:string]; //在文字后面添加图片

 //在文字下标第几个添加图片  0就是文字前面添加图片

      [attri insertAttributedString:string atIndex:0]; 

    // 用label的attributedText属性来使用富文本

    实例化的lable.attributedText = attri;


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM