UILabel添加图片之富文本的简单应用
生活随笔
收集整理的這篇文章主要介紹了
UILabel添加图片之富文本的简单应用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
若想對UILabel添加圖片,那么就需要使用NSMutableAttributedString來定義
先定義一個普通的label
然后對其定義
//創(chuàng)建富文本 NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:@" 我納斯達克市場部撒草卡死你查看售樓處內(nèi) 按時打算打算的撒打算離開的驕傲是是大神快了解到撒開了就對啦可視對講盧卡斯的卡洛斯的驕傲"]; //NSTextAttachment可以將要插入的圖片作為特殊字符處理 NSTextAttachment *attch = [[NSTextAttachment alloc] init]; //定義圖片內(nèi)容及位置和大小 attch.image = [UIImage imageNamed:@"tab_suning"]; attch.bounds = CGRectMake(0, 0, 61, 14); //創(chuàng)建帶有圖片的富文本 NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch]; //將圖片放在最后一位 //[attri appendAttributedString:string]; //將圖片放在第一位 [attri insertAttributedString:string atIndex:0]; //用label的attributedText屬性來使用富文本 lab.attributedText = attri;然后效果如下
若想對圖片添加點擊事件,現(xiàn)在的想法是在label上添加一個透明按鈕,位置大小跟圖片的相同
lab.userInteractionEnabled = YES; UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom]; clearBtn.frame = CGRectMake(0, 12, attch.bounds.size.width, attch.bounds.size.height+3); clearBtn.backgroundColor = [UIColor clearColor]; [clearBtn addTarget:self action:@selector(alertSth) forControlEvents:UIControlEventTouchUpInside]; [lab addSubview:clearBtn];效果如下
轉(zhuǎn)載于:https://www.cnblogs.com/Free-Thinker/p/9587919.html
總結(jié)
以上是生活随笔為你收集整理的UILabel添加图片之富文本的简单应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MATLAB表示非线性系统,matlab
- 下一篇: java开发手册