Obtains the auxiliary function that completely displays the maximum font of the specified text within the specified size range.

Source: Internet
Author: User


Obtains the auxiliary function that completely displays the maximum font of the specified text within the specified size range.

The function is defined as follows:

 
-(INT) getfontsizeforstring :( STD: string) strtext maxfontsize :( INT) maxfontsize forsize :( cgsize) size;

The strtext parameter is the text to be displayed, and maxfontsize is the initial font size. Size is the area range of the text to be displayed. The return value is the most appropriate font number. If the maxfontsize font size cannot fully display the specified text within the specified size range, the Helper function tests the font smaller than this one until it finds the font that can display all the text.


The default font of the auxiliary function isIn fact, the Helvetica font can be improved by specifying the font as a parameter. The font will not be modified, and it will become lazy.


Function implementation:

-(INT) getfontsizeforstring :( STD: string) strtext maxfontsize :( INT) maxfontsize forsize :( cgsize) size {int fontsize = maxfontsize; bool bcontinue = true; while (bcontinue & fontsize> 0) {cgsize showtipssize; uifont * newfont = [uifont fontwithname: @ "Helvetica" Size: fontsize]; nsstring * nstext = [nsstring comment: strtext. c_str () encoding: nsutf8stringencoding]; cgsize originsize; originsize. height = size. height; originsize. width = 10000; showtipssize = [nstext sizewithfont: newfont constrainedtosize: originsize linebreakmode: uilinebreakmodewordwrap]; If (showtipssize. width + 5 <= size. width) {bcontinue = false;} else {fontsize = fontSize-1;} return fontsize ;}

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.