previously encapsulated a number of methods to implement this function, although all achieved the effect, but the code and implementation ideas are relatively complex, and no better than the method I would like to say, this is my whim today, measuring the width of the font, and then based on this point to calculate the Uilabel height. Here I use the fontsize is 13, I measured its height, width is 15 pixels, if you use other font, you need to test the font width, height, paste the code below.
First, implement the code
//The height of the label at the time of the declaration can be arbitrarily set, since the height after calculation will also need to be set onceUILabel *label = [[UILabel alloc]initwithframe:cgrectmake (Ten,0, device_width- -, Max)]; Label.text=@"The Story of "Wild Mind" unfolds: 586 years of great desolation, Shennong Feather, Shinti of the position of floating, causing gold, wood, water, fire, earth five clan war. Shen Nong descendants Tuoba and Yi-Chi-June jointly against the invaders, the two armies confrontation, the Earth is in the middle of the battlefield split, seal Taikoo Demon's skin Mother Earth to reproduce, chaos, the world will die. This version of the concept poster once exposed, immediately triggered a lot of netizens onlookers. The 2015 film and Television circle, set off the network literature adaptation of IP craze: According to "tomb-Raider Notes", "Ghost blows" and other popular IP adaptation of many TV dramas, films have been released. At the beginning of 2016, Ali Pictures also put "big strokes", officially announced the "Wild kee" This IP will also be moved on the big screen. According to the "Savage Kee" project producer Xiatian, the film "Wild Kee" will be a similar World of Warcraft "Azeroth Continent" of the eastern antiquity worldview, "if must do intuitive analogy, Azeroth mainland speak trolls, elves, Earth spirit, dwarf, dwarf, we speak the world of the emperor, the five saint, the Big 10 God, the big barren ten demon female , with "Shan Hai Jing" as the Blueprint, the story plot and through the Chinese legend, the new ancient world to move on the big screen. The visual aesthetic and World outlook of the film will break the traditional Chinese cognition and make the audience feel familiar but subvert. "; Label.backgroundcolor=[Uicolor Redcolor]; Label.textcolor=[Uicolor Whitecolor]; Label.textalignment=Nstextalignmentcenter; Label.linebreakmode=nslinebreakbywordwrapping; Label.font=[uifont systemfontofsize: -]; //below/15 Here 15 is the width of the 13th font, here is the width of the label/15 (the width of a single font) to calculate the width of this label each line can put how many characters intwordcount=label.frame.size.width/ the; //The following is the length of the font used (that is, the number of fonts)/the number of each row that is calculated above to store the font, figure out how many linesCGFloat heightcount= label.text.length/WordCount; //heightcount*15 Below We are the number of rows calculated in the previous step * The height of the font to calculate the final display height of the label[Label Setframe:cgrectmake (Label.frame.origin.x,0, label.frame.size.width,heightcount* the )]; Label.numberoflines=0; [Self Addsubview:label];
Second, the following is the effect (because the width of iphone4 and IPHONE5 is the same, so the effect does not differ)
iOS calculates the Uilabel height based on the number of words