123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- //
- // M80AttributedLabelAttachment.m
- // M80AttributedLabel
- //
- // Created by amao on 13-8-31.
- // Copyright (c) 2013年 www.xiangwangfeng.com. All rights reserved.
- //
- #import "M80AttributedLabelAttachment.h"
- void deallocCallback(void* ref)
- {
-
- }
- CGFloat ascentCallback(void *ref)
- {
- M80AttributedLabelAttachment *image = (__bridge M80AttributedLabelAttachment *)ref;
- CGFloat ascent = 0;
- CGFloat height = [image boxSize].height;
- switch (image.alignment)
- {
- case M80ImageAlignmentTop:
- ascent = image.fontAscent;
- break;
- case M80ImageAlignmentCenter:
- {
- CGFloat fontAscent = image.fontAscent;
- CGFloat fontDescent = image.fontDescent;
- CGFloat baseLine = (fontAscent + fontDescent) / 2 - fontDescent;
- ascent = height / 2 + baseLine;
- }
- break;
- case M80ImageAlignmentBottom:
- ascent = height - image.fontDescent;
- break;
- default:
- break;
- }
- return ascent;
- }
- CGFloat descentCallback(void *ref)
- {
- M80AttributedLabelAttachment *image = (__bridge M80AttributedLabelAttachment *)ref;
- CGFloat descent = 0;
- CGFloat height = [image boxSize].height;
- switch (image.alignment)
- {
- case M80ImageAlignmentTop:
- {
- descent = height - image.fontAscent;
- break;
- }
- case M80ImageAlignmentCenter:
- {
- CGFloat fontAscent = image.fontAscent;
- CGFloat fontDescent = image.fontDescent;
- CGFloat baseLine = (fontAscent + fontDescent) / 2 - fontDescent;
- descent = height / 2 - baseLine;
- }
- break;
- case M80ImageAlignmentBottom:
- {
- descent = image.fontDescent;
- break;
- }
- default:
- break;
- }
-
- return descent;
- }
- CGFloat widthCallback(void* ref)
- {
- M80AttributedLabelAttachment *image = (__bridge M80AttributedLabelAttachment *)ref;
- return [image boxSize].width;
- }
- #pragma mark - M80AttributedLabelImage
- @interface M80AttributedLabelAttachment ()
- - (CGSize)calculateContentSize;
- - (CGSize)attachmentSize;
- @end
- @implementation M80AttributedLabelAttachment
- + (M80AttributedLabelAttachment *)attachmentWith:(id)content
- margin:(UIEdgeInsets)margin
- alignment:(M80ImageAlignment)alignment
- maxSize:(CGSize)maxSize
- {
- M80AttributedLabelAttachment *attachment = [[M80AttributedLabelAttachment alloc]init];
- attachment.content = content;
- attachment.margin = margin;
- attachment.alignment = alignment;
- attachment.maxSize = maxSize;
- return attachment;
- }
- - (CGSize)boxSize
- {
- CGSize contentSize = [self attachmentSize];
- if (_maxSize.width > 0 &&_maxSize.height > 0 &&
- contentSize.width > 0 && contentSize.height > 0)
- {
- contentSize = [self calculateContentSize];
- }
- return CGSizeMake(contentSize.width + _margin.left + _margin.right,
- contentSize.height+ _margin.top + _margin.bottom);
- }
- #pragma mark - 辅助方法
- - (CGSize)calculateContentSize
- {
- CGSize attachmentSize = [self attachmentSize];
- CGFloat width = attachmentSize.width;
- CGFloat height = attachmentSize.height;
- CGFloat newWidth = _maxSize.width;
- CGFloat newHeight = _maxSize.height;
- if (width <= newWidth &&
- height<= newHeight)
- {
- return attachmentSize;
- }
- CGSize size;
- if (width / height > newWidth / newHeight)
- {
- size = CGSizeMake(newWidth, newWidth * height / width);
- }
- else
- {
- size = CGSizeMake(newHeight * width / height, newHeight);
- }
- return size;
- }
- - (CGSize)attachmentSize
- {
- CGSize size = CGSizeZero;
- if ([_content isKindOfClass:[UIImage class]])
- {
- size = [((UIImage *)_content) size];
- }
- else if ([_content isKindOfClass:[UIView class]])
- {
- size = [((UIView *)_content) bounds].size;
- }
- return size;
- }
- @end
|