IOS计算字符串尺寸
.h
#import <Foundation/Foundation.h> @interface NSString (Extension) /** * 返回字符串所占用的尺寸 * * @param font 字体 * @param maxSize 最大尺寸 */ - (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize; @end
。m
#import "NSString+Extension.h" @implementation NSString (Extension) - (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize { NSDictionary *attrs = @{NSFontAttributeName : font}; return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size; } @end
IOS开发