Created
April 10, 2014 03:22
-
-
Save zkwentz/10340070 to your computer and use it in GitHub Desktop.
DTAttributed Label with DTTextAttachment outside UIEdgeInsets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface UIViewController () | |
{ | |
DTAttributedLabel* storyLabel; | |
} | |
- (void)viewDidLoad | |
{ | |
storyLabel.edgeInsets = UIEdgeInsetsMake(20, 20, 20, 20); | |
} | |
- (UIView *)attributedTextContentView:(DTAttributedTextContentView *)attributedTextContentView viewForAttachment:(DTTextAttachment *)attachment frame:(CGRect)frame | |
{ | |
if([attachment isKindOfClass:[DTImageTextAttachment class]]) | |
{ | |
DTLazyImageView *imageView = [[DTLazyImageView alloc] initWithFrame:frame]; | |
imageView.delegate = self; | |
imageView.contentMode = UIViewContentModeScaleAspectFit; | |
// url for deferred loading | |
imageView.url = attachment.contentURL; | |
return imageView; | |
} | |
return nil; | |
} | |
- (void)lazyImageView:(DTLazyImageView *)lazyImageView didChangeImageSize:(CGSize)size | |
{ | |
NSURL *url = lazyImageView.url; | |
NSPredicate *pred = [NSPredicate predicateWithFormat:@"contentURL == %@", url]; | |
// update all attachments that matching this URL | |
for (DTTextAttachment *oneAttachment in [storyLabel.layoutFrame textAttachmentsWithPredicate:pred]) | |
{ | |
oneAttachment.originalSize = size; | |
CGFloat aspectRatio = 320 / oneAttachment.displaySize.width; | |
CGSize maxSize = CGSizeMake(oneAttachment.originalSize.width * aspectRatio, oneAttachment.originalSize.height * aspectRatio); | |
oneAttachment.displaySize = maxSize; | |
} | |
// need to reset the layouter because otherwise we get the old framesetter or cached layout frames | |
storyLabel.layouter = nil; | |
// here we're layouting the entire string, | |
// might be more efficient to only relayout the paragraphs that contain these attachments | |
[storyLabel relayoutText]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment