Text Height Calculation: This method is suitable for setting the same size as text depending on the length of the text
//************************************************************************************************************* ***//
//************************************************************************************************************* **//
Calculate text Height ********************************************************//
//************************************************************************************************************* **//
Stupid programming **********************************/ /
//************************************************************************************************************* **//
/* *
* Param:string: Text that needs to be computed
* Param:font: Font size for text display
* Param:maxsize: The range of text display
* Return: The true width of the text occupied
*/
-(Cgsize) sizewithstring: (NSString *) string font: (Uifont *) font maxSize: (cgsize) maxsize{
Nsdictionary *dict = @{nsfontattributename:font};
Cgsize size = [string Boundingrectwithsize:maxsize options:nsstringdrawinguseslinefragmentorigin attributes:dict Context:nil].size;
return size;
}
//************************************************************************************************************* ***//
-(CGRect) Boundingrectwithsize: (cgsize) Size options: (nsstringdrawingoptions) Options attributes: (Nsdictionary *) Attributes context: (Nsstringdrawingcontext *) Context Ns_available_ios (7_0);
//size: 用于计算文本绘制时占据的矩形块。日常使用经常是宽度设置为一个固定值,然后高度为CGFLOAT_MAX(无限大的一个数值)
//options:文本绘制时附加选项,可以附加使用。
这里是官方API文档
typedef ns_options (Nsinteger, nsstringdrawingoptions) {
Nsstringdrawingtruncateslastvisibleline = 1 << 5,//If the text content exceeds the specified rectangle limit, the text is truncated and an ellipsis is added after the last character. (truncates and adds the ellipsis character to the last visible line if the text doesn ' t fit into the bounds specified. Ignored if Nsstringdrawinguseslinefragmentorigin is not also set.)
Nsstringdrawinguseslinefragmentorigin = 1 << 0,//This option is ignored (the specified origin is the line fragment origin, not the Base line origin)
nsstringdrawingusesfontleading = 1 << 1,//Use line spacing when calculating row height, font size + line spacing = row height (Uses the font leading for calculating line Heig Hts
Nsstringdrawingusesdevicemetrics = 1 << 3,//Use the text size on the picture instead of the text size on the print (Uses image glyph bounds instead of typographic Bou Nds
} ns_enum_available_ios (6_0);
//attributes:文字的属性。必须设置字体的大小,注意这时是字典下的UIFont类型的。//comtext: context上下文,常设为nil。
See iOS development Octopus Brother ********************************************************* //
http://www.jianshu.com/p/155f9065f6d1********************************* *******//
iOS text height calculation