一,效果图。

二,代码。

复制代码
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //根据内容自动调整高度
    NSString *str = @"公元前3000年,印度河流域的居民的数字使用就已经比较普遍,居民们采用了十进位制的计算法。";
    UIFont *font = [UIFont systemFontOfSize:13];
    CGSize size = CGSizeMake(200,2000);
    CGRect labelRect = [str boundingRectWithSize:size options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)  attributes:[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName] context:nil];
    
    //UILabel
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50,100, labelRect.size.width, labelRect.size.height)];
    label.text = str;
    label.backgroundColor=[UIColor redColor];
    label.font = [UIFont systemFontOfSize:13];
    label.numberOfLines=0;
    [self.view addSubview: label];
}
复制代码
内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!