iOS common technology-Calculate label height

Source: Internet
Author: User

//
Viewcontroller.m
Calculate label Height
//
Created by Big Huan on 16/1/19.
COPYRIGHT©2016 year BJSXT. All rights reserved.
//

#import "ViewController.h"

@interface Viewcontroller ()

@end

@implementation Viewcontroller

-(void) Viewdidload {
[Super Viewdidload];

[Self example1];

Multi-line text calculation height

CGFloat width = self.view.frame.size.width-40;

NSString * string = @ "Remember when you said the house was the only castle with the rice-water river continues to run slightly smile childhood dream I know don't cry let the Firefly take you escape the country's ballad forever Rely on Go home back to the original good";

Nsmutableparagraphstyle * paragraph = [[Nsmutableparagraphstyle alloc] init];
paragraph.linespacing = 10;

Nsdictionary * dict = @{nsfontattributename:[uifont systemfontofsize:30],
Nsparagraphstyleattributename:paragraph};

nsattributedstring * attribute = [[Nsattributedstring alloc]
Initwithstring:string Attributes:dict];
Be sure to first determine the width and then calculate the size based on the width and font
Cgsize size = [string Boundingrectwithsize:cgsizemake (width, maxfloat) options:nsstringdrawinguseslinefragmentorigin Attributes:dict context:nil].size;

UILabel * label = [[UILabel alloc] init];
Label.bounds = CGRectMake (0, 0, size.width, size.height);
Label.center = Self.view.center;
Label.numberoflines = 0;
Label.attributedtext = attribute;
Label.backgroundcolor = [Uicolor Yellowcolor];
[Self.view Addsubview:label];

}

-(void) example1 {
Gets the size of a row of labels

NSString * string = @ "Black Cat Sheriff";
Nsdictionary * dict = @{nsfontattributename:[uifont systemfontofsize:50]};
Cgsize size = [string sizewithattributes:dict];

UILabel * label = [[UILabel alloc] init];
Label.text = string;
Label.bounds = CGRectMake (0, 0, size.width, size.height);
Label.center = Self.view.center;
Label.font = [Uifont systemfontofsize:50];
Label.backgroundcolor = [Uicolor Orangecolor];
[Self.view Addsubview:label];
}
@end

iOS common technology-Calculate label height

Related Article

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.