iOS text height calculation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.