计算字符串的高度有很多种,这里写下最常用的简单计算字符串的高度
// NSString+NSStringExt.h
// UIFontSize
// Created by mac on 15/11/14.、
// Copyright (c) 2015年 宁丽环. All rights reserved.
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface NSString (NSStringExt)
//对象方法
-(CGSize)sizeOfTextWithMaxSize:(CGSize)maxSize font:(UIFont *)font;
//类方法
+(CGSize)sizeWithText:(NSString * )text maxSize:(CGSize)maxSize font:(UIFont*)font;
@end
// NSString+NSStringExt.m
// UIFontSize
// Created by mac on 15/11/14.
// Copyright (c) 2015年 宁丽环. All rights reserved.
#import "NSString+NSStringExt.h"
@implementation NSString (NSStringExt)
//实现减号方法
-(CGSize)sizeOfTextWithMaxSize:(CGSize)maxSize font:(UIFont *)font
{
NSDictionary * attrs = @{NSFontAttributeName : font};
return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}
//实现类方法
+(CGSize)sizeWithText:(NSString *)text maxSize:(CGSize)maxSize font:(UIFont *)font
{
//maxSize CGSize 宽不指定 ,高 MAXFLOAT
return [text sizeOfTextWithMaxSize:maxSize font:font];
}
@end
网友评论