Label借助富文本显示图片

1.即时通讯项目中语音消息UI的实现,样式如图:

  • 借助富文本在UILabel中显示图片和文字
// 1.创建一个可变的富文本
NSMutableAttributedString *voiceAttr = [[NSMutableAttributedString alloc] init];
if ([self.reuseIdentifier isEqualToString:@"receiveCell"]) { // 接收方的label:图片 + 时间 // 2.创建图片富文本附件
NSTextAttachment *imageAttachment = [[NSTextAttachment alloc] init];
UIImage *voiceImage = [UIImage imageNamed:@"chat_receiver_audio_playing_full"];
// 2.1指定富文本附件的图片
imageAttachment.image = voiceImage;
// 2.2设置图片的frame,-7是为了图片和文字横向对齐一点
imageAttachment.bounds = CGRectMake(0, -7, 30, 30);
// 2.3将图片富文本附件添加到富文本中
NSAttributedString *imgAttr = [NSAttributedString attributedStringWithAttachment:imageAttachment];
// 3.将图片富文本添加到可变的富文本中
[voiceAttr appendAttributedString:imgAttr];
// 4.创建时间文字的富文本
EMVoiceMessageBody *voiceBody = [message.messageBodies firstObject];
NSInteger time = voiceBody.duration;
NSAttributedString *timeAtt = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@" %ld″",time]];
[voiceAttr appendAttributedString:timeAtt];
} else { // 发送方的label: 时间 + 图片 // 4.创建时间文字的富文本
EMVoiceMessageBody *voiceBody = [message.messageBodies firstObject];
NSInteger time = voiceBody.duration;
NSAttributedString *timeAtt = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%ld″ ",time]];
[voiceAttr appendAttributedString:timeAtt];
// 2.创建图片富文本
NSTextAttachment *imageAttachment = [[NSTextAttachment alloc] init];
UIImage *voiceImage = [UIImage imageNamed:@"chat_sender_audio_playing_full"];
imageAttachment.image = voiceImage;
imageAttachment.bounds = CGRectMake(self.msgLabel.bounds.size.width - 30, -7, 30, 30);
NSAttributedString *imgAttr = [NSAttributedString attributedStringWithAttachment:imageAttachment];
// 3.将图片富文本添加到可变的富文本中
[voiceAttr appendAttributedString:imgAttr];
}

2.即时通讯发送图片 -- 在UILabel中添加了UIImageView,虽然设置了UILabel的尺寸,但是并没有什么卵用.UILabel尺寸没变,图片也显示的不全

  • 使用富文本当做占位符的作用,先将UILabel撑到显示图片的大小
  • 最后将UIImageView添加到UILabel中问题就解决了
EMImageMessageBody *imageBody = [self.message.messageBodies firstObject];

    // 1.给label设置富文本,目的:占位,将label撑大,足够显示缩略图
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.bounds = (CGRect){0, 0, imageBody.thumbnailSize};
// 2.给label添加富文本
self.msgLabel.attributedText = [NSAttributedString attributedStringWithAttachment:attachment]; // 3.将UIImageView添加到UILabel中
[self.msgLabel addSubview:self.msgImageView];
NSString *path; NSFileManager *manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:[imageBody thumbnailLocalPath]]) { // 本地有图片的情况 path = imageBody.thumbnailLocalPath; [self.msgImageView sd_setImageWithURL:[NSURL fileURLWithPath:path] placeholderImage:nil];
} else { // 本地没有图片的情况
path = imageBody.thumbnailRemotePath; [self.msgImageView sd_setImageWithURL:[NSURL URLWithString:path] placeholderImage:nil];
}
// 4.设置图片的位置与尺寸
self.msgImageView.frame = (CGRect){0, 0, imageBody.thumbnailSize};

iOS开发小技巧--即时通讯项目:使用富文本在UILabel中显示图片和文字;使用富文本占位显示图片的更多相关文章

  1. iOS开发小技巧--即时通讯项目:消息发送框(UITextView)高度的变化; 以及UITextView光标复位的小技巧

    1.即时通讯项目中输入框(UITextView)跟随输入文字的增多,高度变化的实现 最主要的方法就是监听UITextView的文字变化的方法- (void)textViewDidChange:(UIT ...

  2. iOS开发小技巧 - runtime适配字体

    iOS开发小技巧 - runtime适配字体 版权声明:本文为博主原创文章,未经博主允许不得转载,有问题可联系博主Email: liuyongjiesail@icloud.com 一个iOS开发项目无 ...

  3. iOS开发小技巧 - UILabel添加中划线

    iOS开发小技巧 遇到的问题: 给Label添加中划线,然后并没有效果 NSString *str = [NSString stringWithFormat:@"合计金额 ¥%.2f&quo ...

  4. iOS开发融云即时通讯集成详细步骤

    1.融云即时通讯iOS SDK下载地址   http://rongcloud.cn/downloads  选择iOS   SDK下载 2.进行应用开发之前,需要先在融云开发者平台创建应用,如果您已经注 ...

  5. 【转】IOS开发小技巧

    1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [ ...

  6. iOS开发小技巧 -- tableView-section圆角边框解决方案

    [iOS开发]tableView-section圆角边框解决方案 tableView圆角边框解决方案 iOS 7之前,图下圆角边框很容易设置 iOS 7之后,tableviewcell的风格不再是圆角 ...

  7. ios开发小技巧之提示音播放与震动

    在ios开发中,有时候我们需要频繁播放某种提示声音,比如微博刷新提示音.QQ消息提示音等,对于这些短小且需要频繁播放的音频,最好将其加入到系统声音(system sound)里. 注意: 需要播放的音 ...

  8. iOS开发小技巧--微博项目中的键盘工具条

    微博项目中的键盘工具条 项目中的键盘工具条不能使用inputAccessoryView,因为inputAccessoryView不能实现键盘隐藏的时候,工具条还显示在眼前,如图: 所以,果断决定将工具 ...

  9. iOS开发小技巧--适当的清空模型中的某个数据,达到自己的需求,记得最后将数据还原(百思项目评论页面处理最热评论)

    一.项目需求,显示所有贴的时候,需要显示最热评论,但是点击进入相应帖子后,最热评论的label不要显示,如图: 解决方案 -- 该暂时保存的暂时保存,该清空的清空                   ...

随机推荐

  1. Core Dump [Linux]

    I have known Core-Dump for a long time. But I just know it little. Now, I am about to study it.

  2. 微信支付 总提示get_brand_wcpay_request:fail 也不跳转支付页面 的解决方案

    最近在做微信支付,帮客户部署好环境后,测试微信支付,发现点击支付后老是提示: get_brand_wcpay_request:fail, 于是找到代码中调用微信支付的代码段: WeixinJSBrid ...

  3. 翻译《Writing Idiomatic Python》(五):类、上下文管理器、生成器

    原书参考:http://www.jeffknupp.com/blog/2012/10/04/writing-idiomatic-python/ 上一篇:翻译<Writing Idiomatic ...

  4. TopCoder SRM 639 Div.2 500 AliceGameEasy --乱搞

    题意: 一个游戏有n轮,有A和B比赛,谁在第 i 轮得胜,就获得 i 分,给出x,y,问A得x分,B得y分有没有可能,如果有,输出A最少赢的盘数. 解法: 这题是我傻逼了,处理上各种不优越,要使n*( ...

  5. CF719C. Efim and Strange Grade[DP]

    C. Efim and Strange Grade time limit per test 1 second memory limit per test 256 megabytes input sta ...

  6. AC日记——字符串判等 openjudge 1.7 17

    17:字符串判等 总时间限制:  1000ms 内存限制:   65536kB 描述 判断两个由大小写字母和空格组成的字符串在忽略大小写,且忽略空格后是否相等. 输入 两行,每行包含一个字符串. 输出 ...

  7. QuickFix/J 源代码

    三). 客户化FIX解析 基础知识:以下文章内容描述的前提是已经根据自己的业务规则,生成了符合要求的数据字典,并且使用QuickFix/J自带的 ant 的 jar target生成了客户化的协议解析 ...

  8. 转:Git 求生手册 - 第三章分支工作

    from:http://newbranch.cn/zhi-zuo-fen-zhi-lai-gong-zuo-git-gh-pages-branching/ 来自:片段 实战 说了这么一大堆分支的东西. ...

  9. uva216 Getting in Line

    Computer networking requires that the computers in the network be linked. This problem considers a \ ...

  10. FragmentTransaction.replace() 你不知道的坑

    一.起源: 先看效果,在linearLayout中添加了4个Fragment,然后点击替换一次确替换了两个Fragment,引发了我的研究兴趣: 第一次启动                       ...