Files
leanote-ios/Leanote/controller/auth/UILabel+SuggestSize.m
2015-08-21 23:49:41 +08:00

31 lines
918 B
Objective-C
Executable File

#import "UILabel+SuggestSize.h"
@implementation UILabel (SuggestSize)
- (CGSize)suggestedSizeForWidth:(CGFloat)width
{
if (self.attributedText) {
return [self suggestSizeForAttributedString:self.attributedText width:width];
}
return [self suggestSizeForString:self.text width:width];
}
- (CGSize)suggestSizeForAttributedString:(NSAttributedString *)string width:(CGFloat)width
{
if (!string) {
return CGSizeZero;
}
return [string boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
}
- (CGSize)suggestSizeForString:(NSString *)string width:(CGFloat)width
{
if (!string) {
return CGSizeZero;
}
return [self suggestSizeForAttributedString:[[NSAttributedString alloc] initWithString:string attributes:@{NSFontAttributeName: self.font}] width:width];
}
@end