代码 label自适应 (计算label的宽和高)

- (void)setupLabel {


    UILabel *textLabel = [[UILabel alloc] init];

    textLabel.font = [UIFont systemFontOfSize:16];

    NSString *str = @"CSDN博客-IT技术写作分享平台";

    textLabel.text = str;

    textLabel.lineBreakMode = NSLineBreakByTruncatingTail;

    CGSize maximumLabelSize = CGSizeMake(100, 9999);//labelsize的最大值

    CGSize labelSize = [textLabel sizeThatFits:maximumLabelSize];

    textLabel.frame = CGRectMake(20, 70, labelSize.width, labelSize.height);

    [self.view addSubview:textLabel];
}

你可能感兴趣的:(ios,label,代码自适应)