If the gettextextent function is similar to that of windows

Source: Internet
Author: User

If the GetTextExtent function is similar to that of windows

 

In the process of Interface Programming, we often encounter the need to dynamically calculate the range occupied by a text segment in a specified control. This function can be used in windows.

The GetTextExtent function is implemented, but in the iphone, We must calculate it by ourselves according to the following method.

 

 

Method:

1. First, we need to obtain the font of the control.

 

2. Using the obtained font, we use the NSString function.

-(CGSize) sizeWithFont :( UIFont *) font constrainedToSize :( CGSize) size lineBreakMode :( UILineBreakMode) lineBreakMode

To calculate the size of the occupied area.

 

About: size parameter, size. width is only the maximum value in the X direction, size. height is the maximum value in the Y direction. In general, width is the maximum width of the control minus a little, while height is as large as possible, because the width is usually fixed, but only the height required by the control needs to be calculated.

 

 

The sample code is as follows:

UIFont * labelFont = Label _. font; <br/> CGSize size = [Label _. text sizeWithFont: labelFont constrainedToSize: CGSizeMake (3200000f, 1000.0f) lineBreakMode: UILineBreakModeWordWrap]; <br/> Label _. frame = CGRectMake (251, 39, size. width, size. height ); 

Related Article

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.