There are many ways to calculate the height of a string, and here is the height of the most commonly used simple computed string.
nsstring+nsstringext.h// uifontsize//// Created by Mac on 15/11/14.// Copyright (c) 2015 Ye Shi. All rights reserved.//#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface nsstring ( Nsstringext)//Object Method-(Cgsize) Sizeoftextwithmaxsize: (cgsize) maxSize font: (Uifont *) font;//class method + (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 Ye Shi. All rights reserved.//#import "Nsstring+nsstringext.h" @implementation nsstring (Nsstringext)//implement Minus Method-(cgsize) Sizeoftextwithmaxsize: (cgsize) maxSize font: (Uifont *) font{ nsdictionary * attrs = @{nsfontattributename:font}; return [self boundingrectwithsize:maxsize options:nsstringdrawinguseslinefragmentorigin attributes:attrs context: Nil].size;} Implementation class method + (Cgsize) Sizewithtext: (NSString *) text maxSize: (cgsize) maxSize font: (Uifont *) font{ //maxsize cgsize width unspecified, high maxfloat return [text Sizeoftextwithmaxsize:maxsize Font:font];} @end
Simple calculation of the height of a string