Dynamic calculation of uilable height in IOS7

Source: Internet
Author: User
Tags uikit

. 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 changed on the basis of others.

The following is a detailed implementation:

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); Width unchanged, based on the number of words to calculate the height of the label nsstring *STR = @ "With the Zhang Zhenyue of this old song, as if I went back to the past, back to the university will be the days of separation, with not shed with confusion and they say goodbye." Another year's graduation season, looking at a variety of friends in the lap of graduation photos, graduation travel photos, bachelor's hats flying all over the sky. All kinds of farewell activities in the flowers and applause slowly fade away from the color, in the mind from time and again filled with a variety of thoughts. Leave campus for a whole year, whenever and others 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 wasted time, someone learned knowledge. Some people have learned life experience, but also someone to harvest love, and I harvest only the pure friendship, brotherly love.

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 to the various jobs. To embody one's own value, to be busy with life; 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? Everything can not return to the head, only in the heart silently thinking of you, I hope you are well.

。。 ";//cgsize size = [str sizeWithFont:label.font constrainedtosize:cgsizemake (label.frame.size.width, maxfloat) linebre akmode:nslinebreakbywordwrapping];////Based on calculation results again set Uilabel size//[Label Setframe:cgrectmake (+, 280, size.height)] ;//label.text = str; Label.text = str; [Label Setframe:cgrectmake (+, 280, [Label Contentsize].height)]; [Self.view Addsubview:label];

The staring part above is the old method.

Here are a few things to note:

1, I set the label before calling Contentsize . The Linebreakmode property.
2, the text is not the key to change the line is set Cgsizemake (w,h), the difference is mainly in the W,h value changes.


Demo Address


Dynamic calculation of uilable height in IOS7

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.