//
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