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 );