Created
April 18, 2012 13:18
-
-
Save wess/2413505 to your computer and use it in GitHub Desktop.
CORETEXT Layout
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
#define ARTICLE_IMAGE_BORDER_SIZE 4.0f | |
@implementation LOArticleContentView | |
@synthesize text = _text; | |
@synthesize image = _image; | |
- (id)initWithFrame:(CGRect)frame | |
{ | |
self = [super initWithFrame:frame]; | |
if (self) | |
{ | |
self.backgroundColor = WHITE; | |
self.contentMode = UIViewContentModeRedraw; | |
_text = nil; | |
_image = nil; | |
_imageView = nil; | |
_contentFrame = CGRectInset(frame, PAGE_LAYOUT_CELL_PADDING, PAGE_LAYOUT_CELL_PADDING); | |
_frame = NULL; | |
_framesetter = NULL; | |
} | |
return self; | |
} | |
- (void)dealloc | |
{ | |
CFRelease(_frame); | |
CFRelease(_framesetter); | |
} | |
#pragma mark - Setter methods | |
- (void)setText:(NSString *)text | |
{ | |
_text = text; | |
WCAttributedString *attr = [WCAttributedString attributedString]; | |
attr.font = [UIFont systemFontOfSize:18.0f]; | |
[attr append:_text]; | |
_attributedText = attr.string; | |
} | |
- (void)setImage:(UIImage *)image | |
{ | |
if([_imageView.superview isEqual:self]) | |
{ | |
[_imageView removeFromSuperview]; | |
_imageView = nil; | |
} | |
CGFloat diff = image.size.height - (image.size.height / 2); | |
CGRect imageFrame = CGRectMake(0.0f, 0.0f, diff, diff); | |
_image = [[image ratioScaleToRect:CGRectInset(imageFrame, ARTICLE_IMAGE_BORDER_SIZE, ARTICLE_IMAGE_BORDER_SIZE) fromSize:_image.size] cropImageToSizeFromCenter:CGSizeMake(_contentFrame.size.height, _contentFrame.size.height)]; | |
imageFrame.origin.x = CGRectGetWidth(self.frame) - CGRectGetWidth(imageFrame); | |
imageFrame.origin.y += ARTICLE_CONTENT_PADDING; | |
// imageFrame.origin.y = (_contentFrame.size.height - imageFrame.size.height); | |
_imageView = [[UIImageView alloc] initWithImage:_image]; | |
_imageView.frame = imageFrame; | |
_imageView.autoresizingMask = FLEX_LEFT | FLEX_BOTTOM; | |
[self addSubview:_imageView]; | |
} | |
- (void)drawRect:(CGRect)rect | |
{ | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
CGContextSetTextMatrix(context, CGAffineTransformIdentity); | |
CGContextTranslateCTM(context, 0, rect.size.height); | |
CGContextScaleCTM(context, 1.0f, -1.0f); | |
CGContextSetTextMatrix(context, CGAffineTransformIdentity); | |
CGRect clippingFrame = CGRectZero; | |
if(_image) | |
{ | |
clippingFrame = _imageView.bounds; | |
clippingFrame.size.width = (_imageView.width + (ARTICLE_CONTENT_PADDING * 4)); | |
clippingFrame.origin.x = (rect.size.width - clippingFrame.size.width); | |
clippingFrame.origin.y = (_contentFrame.size.height - clippingFrame.size.height); | |
clippingFrame.size.width += clippingFrame.size.width; | |
} | |
CGMutablePathRef path = CGPathCreateMutable(); | |
CGPathAddRect(path, NULL, rect); | |
CGMutablePathRef imageClippingPath = CGPathCreateMutable(); | |
CGPathAddRect(imageClippingPath, NULL, clippingFrame); | |
CFStringRef keys[] = { kCTFramePathClippingPathAttributeName }; | |
CFTypeRef values[] = { imageClippingPath }; | |
CFDictionaryRef clippingPathDict = CFDictionaryCreate(NULL, (const void **)&keys, (const void **)&values, (sizeof(keys) / sizeof(keys[0])), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); | |
NSArray *clippingPaths = [NSArray arrayWithObject:(__bridge NSDictionary*)clippingPathDict]; | |
NSDictionary *optionsDict = [NSDictionary dictionaryWithObject:clippingPaths forKey:(NSString*)kCTFrameClippingPathsAttributeName]; | |
if(_framesetter == NULL) | |
_framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)_attributedText); | |
_frame = CTFramesetterCreateFrame(_framesetter, CFRangeMake(0, 0), path, (__bridge CFDictionaryRef)optionsDict); | |
CTFrameDraw(_frame, context); | |
CGPathRelease(path); | |
CGPathRelease(imageClippingPath); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment