iOS-UILabel calculates label width based on text and font size; and adaptive height

Download addressGitHub  source code  or  Demo download

Want to get all fonts, as follows:

//获取到所有的字体名称
    NSArray *familyNames = [UIFont familyNames];
    NSLog(@"所有字体名称--%@",familyNames);

 

1. Single line text

For single-line text, use the sizeWithAttributes method directly, as follows:

- (void)viewDidLoad {
    [super viewDidLoad];
    //获取到所有的字体名称
    NSArray *familyNames = [UIFont familyNames];
    NSLog(@"所有字体名称--%@",familyNames);
    UILabel *lbl_text = [[UILabel alloc]init];
    lbl_text.backgroundColor = [UIColor greenColor];
    lbl_text.text = @"我们是一家人";
    // 设置Label的字体 HelveticaNeue  Courier
    UIFont *fnt = [UIFont fontWithName:@"Courier New" size:24.0f];
    lbl_text.font = fnt;
    // 根据字体得到NSString的尺寸
    CGSize size = [lbl_text.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName,nil]];
    // 名字的H
    CGFloat nameH = size.height;
    // 名字的W
    CGFloat nameW = size.width;
    lbl_text.frame = CGRectMake(100,100, nameW,nameH);
    [self.view addSubview:lbl_text];
}

As shown in the picture:

 

Two, multi-line text

First, the numberOfLines of UILabel is set to 0, and then the CGSize is calculated by - (CGRect) boundingRectWithSize :(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context method, the specific code is as follows :

- (void)viewDidLoad {
    [super viewDidLoad];
    //获取到所有的字体名称
    NSArray *familyNames = [UIFont familyNames];
    NSLog(@"所有字体名称--%@",familyNames);
    [self setMultiLine];
}

- (void)setMultiLine
{
    UILabel *lbl_text = [UILabel new];
    lbl_text.font = [UIFont systemFontOfSize:14];
    lbl_text.text = @"文字在语言学中指书面语的视觉形式,古代把独体字叫做“文”,把合体字叫做“字”,如今联合起来叫做“文字”,文字的基本个体叫做“字”。在日常生活中,“文字”还可以指书面语、语言、文章、字等。视觉符号形式,突破口语的时间和空间限制。例如汉字、拉丁字母。";
    lbl_text.backgroundColor = [UIColor greenColor];
    lbl_text.numberOfLines = 0;//多行显示,计算高度
    lbl_text.textColor = [UIColor lightGrayColor];
    CGSize lblSize = [lbl_text.text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
    lbl_text.frame = CGRectMake(10, 100, lblSize.width, lblSize.height);
    [self.view addSubview:lbl_text];
}

As shown in the picture:

Guess you like

Origin blog.csdn.net/MinggeQingchun/article/details/52583709