Ios calculates the UILabel height based on the number of characters, iosuilabel

Source: Internet
Author: User

Ios calculates the UILabel height based on the number of characters, iosuilabel

In the past, many methods for implementing this function were encapsulated to achieve the effect, but the code and Implementation ideas were much more complicated and not better than the method I will talk about below, this is what I came up with today's whim: measure the font width and calculate the UILabel height based on this point. The FontSize I used here is 13. I have measured that the height and width of the FontSize are all 15 pixels. If you use another font, You need to paste the code below from the font width and height under the new test.

I. Implementation Code

// You can set the label height when declaring it, because after calculating the height, you also need to set UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (10, 0, DEVICE_Width-20, 150)]; label. text = @ "The Story of the desert: In the 586 s of the great wilderness, the treasure of God was suspended, leading to the five wars of gold, wood, water, fire, and Earth. The father-in-law of Shennong, the descendant of tuba, and the younger brother Yu joined forces to fight against the invaders. However, the earth split in the center of the battlefield, and the pig Earth that seals the fierce demons will reproduce the chaos and the world will die. Once exposed, this version of the concept poster immediately attracted a large number of netizens. In 2015, Film and Television circles set off a wave of IP address adaptation for online literature: multiple TV series and movies adapted from popular IP addresses such as tomb theft notes and ghost blowing lights were successively released. At the beginning of June 2016, Alibaba film launched a "big trick" and officially announced that the IP address of "The Wild Man" will also be moved to the screen. According to the summer dust, the project producer of the wild man project, the movie Wild Man will be an ancient Oriental world view similar to the World of Warcraft "azazelas continent". "If you have to make an intuitive analogy, on the continent of azelas, we talk about the Titans, elves, Tuling, dwarf, and gnome. We talk about the emperors of the world, five holy women, ten gods of the great land shortage, and ten enchanting women of the great land shortage. We use the mountain sea Sutra as our blueprint, the story draws from the Chinese legends and brings the brand new ancient world to the screen. Both the visual aesthetics and world view of movies will break the traditional Chinese Cognition and make the audience feel familiar but subverted. "; Label. backgroundColor = [UIColor redColor]; label. textColor = [UIColor whiteColor]; label. textAlignment = NSTextAlignmentCenter; label. lineBreakMode = NSLineBreakByWordWrapping; label. font = [UIFont systemFontOfSize: 13]; // The following/15 indicates the width of the font on the 13th. The width of the label/15 indicates the width of a single font) calculate the width of the label. The number of Chinese Characters in each row is int WordCount = label. frame. size. width/15; // The following is the font length (the number of fonts)/The number of fonts stored in each row calculated above, and the total number of rows CGFloat heightCount = label. text. length/WordCount; // heightCount * 15 The following figure shows the final display height of the label by calculating the number of lines * font height calculated in the previous step [label setFrame: CGRectMake (label. frame. origin. x, 0, label. frame. size. width, heightCount * 15)]; label. numberOfLines = 0; [self addSubview: label];

2. The following figure shows the effect (because the iPhone 4 and iPhone 5 have the same width, the effect is not different)

 

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.