iOS UITableView,动态文本和图像一起渲染(NSAttributedString + images) [英] iOS UITableView with dynamic text and images rendered together (NSAttributedString + images)

查看:92
本文介绍了iOS UITableView,动态文本和图像一起渲染(NSAttributedString + images)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是:我在iOS应用程序中有动态内容(例如twits - 虽然这不是推特应用程序),包括文本和图像(主要是图标/表情符号和缩略图)。我想在表格行中同时渲染文本和图像。这里的主要困难是每一行都有不同的大小(使缓存更难),我需要动态计算图像大小以适应每个图像出现的文本(我可以有20个表情符号一个接一个+文本+更多表情符号+等)。

My problem is this: I have dynamic content in an iOS app (such as twits - although this is not a twitter app) that include both text and images (mostly icons/emoticons and thumbnails). I want to render both text and images together in a table row. The main difficulty here is that each row will have a different size (making caching harder), and I need to calculate image size dynamically to fit text around on each image occurrence (I can have like 20 emoticons one next to another + text + more emoticons + etc.).

我一直在寻找,我尝试了一些方法。我最初的想法是使用UIWebView。我能够创建一个每行一个UIWebView的示例应用程序,即使有一些智能NSCache用于预渲染单元格,性能也不是很好,而且我不得不处理UIWebView javascript回调以确定内容是否正确加载。

I've been looking around and I've tried a few approaches. My initial idea was to use UIWebView. I was able to create a sample app with one UIWebView per row, and even with some "smart" NSCache for pre-rendered cells performance was not very good, plus I had to deal with UIWebView javascript callbacks to figure out when content was properly loaded.

我的第二次尝试是覆盖新的UITableViewCell的drawRect。在drawRect方法中,我使用NSAttributedString和NSSelectorFromString来设置每种内容的范围(常规文本,粗体,斜体,不同颜色,图像)。为了适应图像我正在使用CTRunDelegateCallbacks回调(getAscent,getDescent,getWidth)。这样的事情:

My second attempt is to overwrite drawRect for a new UITableViewCell. Inside the drawRect method I'm using NSAttributedString and the NSSelectorFromString to set the ranges for each kind of content (regular text, bold, italic, different color, images). To fit the images I'm using the CTRunDelegateCallbacks callbacks (getAscent, getDescent, getWidth). Something like this:

            CTRunDelegateCallbacks callbacks;
            callbacks.version = kCTRunDelegateVersion1;
            callbacks.getAscent = ascentCallback;
            callbacks.getDescent = descentCallback;
            callbacks.getWidth = widthCallback;

            CTRunDelegateRef delegate = CTRunDelegateCreate(&callbacks, (__bridge void *)imgAttr); // img Attr is a Dictionary with all image info

有关此方法的更多详情,请查看此优秀发布: http:// www.raywenderlich.com/4147/how-to-create-a-simple-magazine-app-with-core-text#

For more details on this approach please check this excellent post: http://www.raywenderlich.com/4147/how-to-create-a-simple-magazine-app-with-core-text#

最后,有两个问题:


  • 这是最好的方法,我能否以良好的性能做到这一点?我想知道是否必须为每个卷轴为每个单元调用drawRect不会很麻烦 - 有什么我可以事先做的吗?我已经玩了一段时间了,它仍然是一个很大的滞后 - 但我还没有尝试在单独的线程中缓存每一行并在cellForRowAtIndexPath中使用它(虽然这可能超过内存使用);

  • Is this the best approach, can I make this with good performance? I'm wondering if having to call drawRect for each cell for every scroll wont be cumbersome - is there anything I could do in advance? I've been playing around for a while, and it's still a big lagged - but I haven't yet tried to cache every single row in a separate thread and use that in cellForRowAtIndexPath (although that may exceed memory use);

我无法找出获得每个tableview单元格行高的最佳方法。在NSAttributedString正确处理了我的所有内容后,如何知道drawRect函数使用的高度?

I can't figure out the best way to obtain the row height for each tableview cell. How can I know the height used by my drawRect function after NSAttributedString has properly processed all my content?

---添加更多信息

在我的drawRect创建所有内容(字符串和图像)后,我正在尝试使用boundingRectWithSize来获得正确的高度:

After my drawRect creates all content (strings and images) I'm trying to use boundingRectWithSize to get the proper height:

CGRect frame = [self.attString boundingRectWithSize:CGSizeMake(320, 10000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading | NSStringDrawingUsesDeviceMetrics context:nil];

如果它只是文本,这可以正常工作,但是当我将图像字形添加到内容时它没有正确计算高度......

This works fine if it's text only, but when I add the image glyphs to the content it does not calculate the height properly...

推荐答案

这篇文章可能会让你朝正确的方向计算行高:

This post may get you going in the right direction for calculating row height:

检索自定义故事板中的原型单元格高度?

至于性能,您是否运行Time Profiler来缩小导致延迟的原因?

As for performance, have you run Time Profiler to narrow down what is causing lag?

这篇关于iOS UITableView,动态文本和图像一起渲染(NSAttributedString + images)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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