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