iOS 自动调整标签高度 [英] iOS auto adjust label height

查看:29
本文介绍了iOS 自动调整标签高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码自动调整 UITableView 中标签的高度.它大部分时间都有效,但某些时候文本会被切断.我的代码有问题吗,或者我需要添加什么?

I'm using the following code to auto adjust the height of a label in a UITableView. It works the majority of the time, but certain times text is cut off. Is there something wrong with my code, or anything else I need to add?

UILabel *textLabel = ((UILabel *)[cell viewWithTag:3]);
textLabel.text = text;

CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX);

CGSize expectedLabelSize = [text sizeWithFont:textLabel.font constrainedToSize:maximumLabelSize lineBreakMode:textLabel.lineBreakMode];

//adjust the label the the new height.
CGRect newFrame = textLabel.frame;
newFrame.size.height = expectedLabelSize.height;
textLabel.frame = newFrame;

推荐答案

在 iOS 7 中 sizeWithFont: constrainedToSize: lineBreakMode: 已被弃用,现在你应该使用:

In iOS 7 sizeWithFont: constrainedToSize: lineBreakMode: is deprecated, now you should use:

 CGSize maxSize = CGSizeMake(296.f, FLT_MAX);
 CGRect labRect = [someText boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:textLabel.font} context:nil];


 textLabel.frame = CGRectMake(0, 0, maxSize.width, labRect.size.height);
 textLabel.text = someText;

这篇关于iOS 自动调整标签高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆