. h file
#import <UIKit/UIKit.h> @interface UILabel (contentsize)-(cgsize) contentsize; @end
. m file
#import "Uilabel+contentsize.h" @implementation UILabel (contentsize)-(cgsize) contentsize { Nsmutableparagraphstyle * Paragraphstyle = [[Nsmutableparagraphstyle alloc] init]; Paragraphstyle.linebreakmode = Self.linebreakmode; Paragraphstyle.alignment = self.textalignment; Nsdictionary * attributes = @{nsfontattributename:self.font, nsparagraphstyleattributename:paragraphstyle}; Cgsize contentsize = [Self.text boundingrectwithsize:cgsizemake (Self.frame.size.width, Maxfloat) options: ( nsstringdrawinguseslinefragmentorigin| nsstringdrawingusesfontleading) attributes:attributes context:nil].size; return contentsize;} @end
This class is modified on the basis of others.
Here are the specific implementations:
UILabel *label = [[UILabel alloc] Initwithframe:cgrectmake (20, 20, 280, 20)]; Label.font = [Uifont boldsystemfontofsize:15.0f]; Uilabel Font Size label.numberoflines = 0; This property must be defined, otherwise Uilabel will not wrap label.textcolor = [Uicolor Whitecolor]; Label.textalignment = Nstextalignmentleft; Text alignment [Label Setbackgroundcolor:[uicolor Redcolor]]; Label.linebreakmode = nslinebreakbywordwrapping; NSLog (@ "%d", label.linebreakmode); The width is constant, the height of the label is calculated according to the number of words nsstring *str = @ "along with this old song of Zhang Zhenyue, as if I went back to the past, back to the university will be the days of separation, with not shed and confused and they say goodbye." Another year of graduation, looking at a variety of sunshine graduation photos of friends, graduation travel photos, bachelor hats flying all over the sky, all kinds of farewell activities in the flowers and applause slowly fade away from the color, in the heart is filled with a variety of thoughts. Leave campus for a whole year, whenever and other people talk about the alma mater all kinds of pride, college life all kinds of colorful, life experience a variety of rich, in fact, the heart is still left with all kinds of regrets; four years of wasted time, some harvest knowledge, some people harvest life experience, but also someone harvest love, and I only harvest that pure friendship, brother For a whole year, remember when we were all not, classmate sentiment, brother sentiment, mixed with alcohol flow to the heart, 1.8-meter a few big boys cry splinters, hug say goodbye, after we left the familiar campus, rushed in various jobs, to reflect their value, for life and busy; Some people really will never see again. The first anniversary of graduation, are you all right? Is the work going well? Do you have a liking to study? All can not return to the head, only in the heart silently thinking of you, hope you are well ... ";//cgsize size = [str sizeWithFont:label.font constrainedtosize:cgsizemake (label.frame.size.wiDTH, Maxfloat) linebreakmode:nslinebreakbywordwrapping];////Reset the dimensions of the Uilabel according to the calculation//[Label Setframe:cgrectmake (20, 1 0, 280, size.height)];//label.text = str; Label.text = str; [Label Setframe:cgrectmake (+, 280, [Label Contentsize].height)]; [Self.view Addsubview:label];
The comment section above is the old method.
Here are a few things to note:
1, I set the label before calling Contentsize . The Linebreakmode property.
2, text in the end of the key is to change the line is set Cgsizemake (w,h), the difference is mainly in w,h value modification.
Demo Address