自动获取tf尺寸

#pragma mark-customMethod----Gethight

自动获取tf尺寸_第1张图片

-(float)gethight:(NSString*)text

{

CGSizesize=[textsizeWithFont:[UIFontsystemFontOfSize:17]constrainedToSize:CGSizeMake(160,MAXFLOAT)lineBreakMode:NSLineBreakByWordWrapping];

returnsize.height;

}

NSDictionary *dic=@{      UIFont systemFontofSize:17}



Xcode 7 中使用的获取字符串size的方式:

+(CGSize)gettextSizeWithText:(NSString *)text andStringFloat:(float)strfloatnumber andwidth:(float)with

{

NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:strfloatnumber]};

CGSize size = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, 10000) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;

if(size.width>with) {

size = [text boundingRectWithSize:CGSizeMake(with, MAXFLOAT) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;

}

returnsize;

}

你可能感兴趣的:(自动获取tf尺寸)