label中添加圖片


  • 創建NSTextAttachment的對象,用來裝在圖片
  • NSTextAttachment對象的image屬性設置為想要使用的圖片
  • 設置NSTextAttachment對象bounds大小,也就是要顯示的圖片的大小
  • [NSAttributedString attributedStringWithAttachment:attch]方法,將圖片添加到富文本上

    //創建富文本字符串
    NSMutableAttributedString *mAString = [[NSMutableAttributedString alloc] initWithString:@"你好你好你好你好你好你好你你好你好你好你好你好你好你你好你好你好你好你好你好你"];
    //創建圖片
    NSTextAttachment *attch = [[NSTextAttachment alloc] init];
    attch.image = [UIImage imageNamed:@"ding"];
    attch.bounds = CGRectMake(0, -2, 16, 16);
    //將圖片加入富文本
    NSAttributedString *imgString = [NSAttributedString attributedStringWithAttachment:attch];
    [mAString appendAttributedString:imgString];
    
    _Alabel.attributedText = mAString;
 
效果圖:
 
原文鏈接:http://www.jianshu.com/p/5828173adc3a


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM