kishikawakatsumi / SECoreTextView

SECoreTextView is multi style text view.
MIT License
943 stars 143 forks source link

Can't render only images in iOS 6 #39

Open hahv opened 10 years ago

hahv commented 10 years ago

I want to render only images with SECoreTextView. I have an attributedString: ** (three \ character without format) and and array images. I render these images using SECoreTextView with this helper method:

+(void) insertImageToTextView:(SETextView*)textView fromComponents:(RTComponents*) components {

    for (NSUInteger i = 0;  i < components.images.count; i++) {

        NSMutableDictionary* imageAttributes = [components.images objectAtIndex:i];

        UIImage* image = [UIImage imageNamed:imageAttributes[@"imgSrc"]];
        NSInteger imgIndex = [imageAttributes[@"imgIndex"] unsignedIntegerValue];

        if (image) {
            [textView addObject:image size:CGSizeMake(image.size.width, image.size.height) replaceRange:NSMakeRange(imgIndex, 1)];
        }
    }

}

Add Image to textview

    [SETextViewHelper insertImageToTextView:seTextView fromComponents:data];
//attributedString is ***
    [richTextView setAttributedText:[SETextViewHelper attributedStringFromComponents:attributedString]];

IOS 7, everything is OK but in iOS 6 the problem is SETextLayout will return wrong number line of CTFrame (in this case CTFrameGetLines(_frame) return 0) so no thing is rendered. I can fix this by using the trick that append original attributedString with an character (any character, in my case is space character) and do the same thing above. After do this trick, all images will be rendered and everything is fine.

This issue is something happened like this: http://stackoverflow.com/questions/18703672/ios-method-ctframegetlines-return-empty-array

Please see these images for more details:

Resut with origin String ***

Result after do a trick, orginStr with append space character at the end

imyellow commented 10 years ago

THANK YOU VERY MUCH!!! VERY USEFUL!!