Dynamic Calculation of UILable height in IOS7

Source: Internet
Author: User

. H file

#import 
 
   @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 based on others.

The specific implementation is as follows:

UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (20, 20,280, 20)]; label. font = [UIFont boldSystemFontOfSize: 15.0f]; // The font size label of UILabel. numberOfLines = 0; // This attribute must be defined; otherwise, UILabel will not wrap the label. textColor = [UIColor whiteColor]; label. textAlignment = NSTextAlignmentLeft; // text alignment mode [label setBackgroundColor: [UIColor redColor]; label. lineBreakMode = NSLineBreakByWordWrapping; NSLog (@ "% d", label. li NeBreakMode); // The width remains unchanged. Calculate the label height NSString * str = @ based on the number of words. This old song is accompanied by Zhang zhenyue, as if I have returned to the past, return to the days before the university, and say goodbye to them with perseverance and confusion. In another year's graduation season, I watched my friends share graduation photos, graduation travel photos, and scholar's hats fly in the sky. Various farewell activities slowly fade away from the color in flowers and applause, and my heart is filled with various thoughts from time to time. I have been away from campus for a whole year. Whenever I talk to others about my Alma Mater's pride, college life, and life experiences, I still have all kinds of regrets in my heart, some people have gained knowledge, some have gained experience in life, and some have gained love. What I have gained is pure friendship and brotherhood. For a whole year, I still remember how much we were reluctant to give up at that time, and our friendship and brotherhood were mixed with alcohol. The big boys who were about one meter or more were crying and talking about goodbye; later, we left the familiar campus and went to various jobs to reflect our own values and stay busy for our lives. Some people will never see each other. The first anniversary of graduation. Are you okay? Is the job still going well? Are you still comfortable with learning? Everything cannot go back. I can only think about you silently in my heart. I hope you will be well... "; // CGSize size = [str sizeWithFont: label. font constrainedToSize: CGSizeMake (label. frame. size. width, MAXFLOAT) lineBreakMode: NSLineBreakByWordWrapping]; // reset the UILabel size based on the calculation results. // [label setFrame: CGRectMake (20, 10,280, size. height)]; // label. text = str; label. text = str; [label setFrame: CGRectMake (20, 20,280, [label contentSize]. height)]; [self. view addSubview: label];

The comment above is the old method.

Note the following points:

1. Set the label. lineBreakMode Attribute before calling contentSize.
2. The key to changing text without line breaks is to set CGSizeMake (w, h). The difference is mainly in modifying the values of w and h.


Demo address

 

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.