Use classification, NSString, add a method.
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface nsstring (Height) + (cgsize) GetRect: ( NSString *) textstr andwidth: (cgsize) size Andfont: (Uifont *) font; @end
#import "Nsstring+height.h" @implementation nsstring (Height) + (cgsize) GetRect: (NSString *) textstr andwidth: (cgsize) Size Andfont: (Uifont *) font{ cgrect rect = [textstr boundingrectwithsize:size options: Nsstringdrawinguseslinefragmentorigin Attributes:@{nsfontattributename:font} Context:nil]; return rect.size; } @end
Use:
Cgsize size = [NSString getrect:str andwidth:cgsizemake (IOS_SCREEN.SIZE.WIDTH/2-Ten, Cgfloat_max) AndFont:[UIFont SYSTEMFONTOFSIZE:14]];
which
STR is the string that will be computed,
IOS_SCREEN.SIZE.WIDTH/2-10 is the width,
[Uifont systemfontofsize:14] is the setting font size
These need themselves to be written according to circumstances;
IOS customizes the height of controls such as labels based on the number of strings