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

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

问题描述

我的问题是:我在 iOS 应用程序(例如 twits - 尽管这不是 Twitter 应用程序)中包含文本和图像(主要是图标/表情符号和缩略图)的动态内容.我想在表格行中同时呈现文本和图像.这里的主要困难是每一行都有不同的大小(使缓存更难),我需要动态计算图像大小以适应每个图像出现的文本(我可以有 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 + 图像)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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