Calculate the height of text
In two cases, the specified text has only 1 lines and multiple lines, and the size,options that can be written to return a string usually use nsstringdrawinguseslinefragmentorigin, so that the entire text will calculate the entire text height as a single-line text rectangle
① Text Display Line
-(Cgsize) Sizeonelinetext: (NSString *) text font: (Uifont *) font{ = [text boundingrectwithsize: Cgsizemake (Cgfloat_max, Cgfloat_max) options:nsstringdrawinguseslinefragmentorigin attributes:@{ Nsfontattributename:font} context:nil].size; return textSize;}
② text display multiple lines, you need to specify the text width
-(Cgsize) Sizemultilinetext: (NSString *) text font: (Uifont *) Font width: (cgfloat) width{ = [text Boundingrectwithsize:cgsizemake (width, cgfloat_max) Options:nsstringdrawinguseslinefragmentorigin Attributes:@{nsfontattributename:font} context:nil].size; return textSize;}
This method can be used within a custom button to calculate the position of text and image
Calculate the height of text