UILabel adaptive height, width, UILabel adaptive, width

Source: Internet
Author: User

UILabel adaptive height, width, UILabel adaptive, width

Adaptive height based on Label and font size

- (void)getHeightWithLabel:(UILabel *)label andFontSize:(CGFloat)size{    label.numberOfLines = 0;    NSMutableAttributedString *labelString = [[NSMutableAttributedString alloc] initWithString:label.text];    [labelString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:size] range:NSMakeRange(0, [labelString length])];    CGRect labelStringRect = [labelString boundingRectWithSize:CGSizeMake(self.view.frame.size.width-label.frame.origin.x*2, 9999) options:NSStringDrawingUsesLineFragmentOrigin context:nil];    CGRect labelRect = label.frame;    labelRect.size.height = labelStringRect.size.height;    label.frame = labelRect;    label.attributedText = labelString;}

Adaptive width based on Label and font size

- (void)getWidthWithLabel:(UILabel *)label andFontSize:(CGFloat)size{    label.numberOfLines = 0;    NSMutableAttributedString *labelString = [[NSMutableAttributedString alloc] initWithString:label.text];    [labelString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:size] range:NSMakeRange(0, [labelString length])];    CGRect labelStringRect = [labelString boundingRectWithSize:CGSizeMake(9999, label.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin context:nil];    CGRect labelRect = label.frame;    labelRect.size.width = labelStringRect.size.width;    label.frame = labelRect;    label.attributedText = labelString;}

 

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.