UILabel内部的图像 [英] Images inside of UILabel

查看:142
本文介绍了UILabel内部的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力找到在UILabels内部显示图像的最佳方式(事实上,如果它支持没有麻烦的图像,我不介意切换到UILabel以外的其他东西)

I'm trying to find the best way to display images inside of UILabels (in fact, I wouldn't mind switching to something other than UILabel if it supports images with no hassle)

场景是:


  • 我有一个包含数百个单元格的表格视图,UILabel是每个单元格的主要组成部分

  • 我分配给每个单元格的文本包含需要解析并表示为图像的字符序列

简单来说,想象一下即时信使的TableView会解析所有:),:(,:D等替换相应的笑脸图像

In simpler words, imagine a TableView of an instant messenger that parses replaces all ":)", ":(", ":D" etc with corresponding smiley images

任何输入都将不胜感激!

Any input would be greatly appreciated!

推荐答案

一种方法是使用UIWebView而不是UILabel。将每个表情符号替换为< img src = ...> 标记,其中图像位于您的包中。使用loadHTMLString填充UIWebView并传递<$的URL C $ C> [一个NSBundle mainBundle] resourcePath] 作为baseURL。

One approach would be to use a UIWebView instead of a UILabel. Replace each emoticon with an <img src=...> tag where the image is in your bundle. Use loadHTMLString to populate the UIWebView and pass a URL for [[NSBundle mainBundle] resourcePath] as the baseURL.

另一种更难以手动将图像放在UILabel上方的方法。留下表情符号会出现的几个空格。测量文本并在UILabel上方放置UIImageView用于每个表情符号。

Another, more difficult approach to manually place images above the UILabel. Leave a few spaces where the emoticon would show up. Measure out the text and place a UIImageView above the UILabel for each emoticon.

您还可以将CoreGraphics用于所有文本和图像布局。将字符串分解为文本和表情符号。在绘制每个字符串时,CGContext将更新下一个绘制的文本位置。当你到达表情符号时,绘制适当的图像并手动前进文本位置。

You could also use CoreGraphics for all the text and image layout. Break the strings into text and emoticons. As you draw each string, the CGContext will update the text position where it will draw next. When you get to an emoticon, draw the appropriate image and advance the text position manually.

使用UIWebView可能是最难编码的,并且还有额外的支持奖励风格的文字。除非您跳过表视图并在单个UIWebView中执行所有操作,否则它也是最耗费资源的。你可以通过使用javascript在重用单元格时加载内容来加快速度。

Using a UIWebView is probably the least difficult to code and has the added bonus of supporting styled text. It is also the most resource intensive unless you skip the table view and do everything in a single UIWebView. You can make it a little faster by using javascript to load content when reusing cells.

如果速度成为一个问题,使用CoreGraphics可能是速度最快,资源最少的。

Using CoreGraphics is probably the fastest and least resource intensive if speed becomes an issue.

这篇关于UILabel内部的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆