iOS UITableView with dynamic text and images rendered together (NSAttributedString + images)

后端 未结 2 2062
有刺的猬
有刺的猬 2020-12-06 14:58

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 t

相关标签:
2条回答
  • 2020-12-06 15:14

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

    Retrieve custom prototype cell height from storyboard?

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

    0 讨论(0)
  • 2020-12-06 15:15

    I ran into the same issue und solved it today. I am drawing custom objects between the text with the CTRunDelegateCallbacks, but boundingRectWithSize:options:context:did not give me the correct height. (It ignores my custom objects).

    Instead I found a lower level API in Core Text, which fulfils all requirements and is totally correct. I created a category for that issue. Have fun using it! :-)

    Header file:

    #import <Foundation/Foundation.h>
    
    @interface NSAttributedString (boundsForWidth)
    
    - (CGRect)boundsForWidth:(float)width;
    
    @end
    

    Implementation file:

    #import "NSAttributedString+boundsForWidth.h"
    #import <CoreText/CoreText.h>
    
    @implementation NSAttributedString (boundsForWidth)
    
    - (CGRect)boundsForWidth:(float)width
    {
        CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)self);
    
        CGSize maxSize = CGSizeMake(width, 0);
    
        CGSize size = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, 0), NULL, maxSize, nil);
    
        CFRelease(framesetter);
    
        return CGRectMake(0, 0, ceilf(size.width), ceilf(size.height));
    }
    
    @end
    
    0 讨论(0)
提交回复
热议问题