UILabel根据要显示的文本自动调整大小

Muh*_*had 55 objective-c uitextview uilabel ios

我正在开发一个应用程序,我需要根据要显示的文本自动调整文本区域.

首先,我不确定这是否应该使用UILabel(逻辑上是显示静态文本的最佳选择,在我的情况下)或UITextView.

我多想用它?
我想简单地使用Text初始化我的标签或文本视图.相反,我首先定义框架,然后限制我在该区域的文本.

如果您能提出正确的解决方案,那将是一个很大的帮助.

我浏览了文档和其他参考资料,但没有找到太多可以帮助我的地方,或者我可以忽略它.

Muh*_*had 97

这种sizeToFit方法效果很好.

我做了以下.

UILabel *testLabel =[[UILabel alloc] initWithFrame:CGRectMake(6,3, 262,20 )]; // RectMake(xPos,yPos,Max Width I want, is just a container value);

NSString * test=@"this is test this is test inthis is test ininthis is test inthis is test inthis is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...";

testLabel.text = test;
testLabel.numberOfLines = 0; //will wrap text in new line
[testLabel sizeToFit];

[self.view addSubview:testLabel];
Run Code Online (Sandbox Code Playgroud)

  • 水平调整视图而不是垂直调整大小 (15认同)

j_f*_*yre 29

您可以找到文字大小:

CGSize textSize = [[myObject getALongText] 
                    sizeWithFont:[UIFont boldSystemFontOfSize:15] 
                    constrainedToSize:CGSizeMake(maxWidth, 2000)
                    lineBreakMode:UILineBreakModeWordWrap];
Run Code Online (Sandbox Code Playgroud)

然后你可以创建你UILabel喜欢的:

UILabel * lbl = [[UILabel alloc] initWithFrame:CGRectMake(0,0,textSize.width, textSize.height];
[lbl setNumberOfLines:0];
[lbl setLineBreakMode:UILineBreakModeWordWrap];
[lbl setText:[myObject getALongText]];
Run Code Online (Sandbox Code Playgroud)

  • 错误:你不应该问NSString当你在UILabel中绘制它时会有多大的尺寸. (2认同)

Seg*_*gev 14

在Swift中:

testLabel = UILabel(frame: CGRectMake(6, 3, 262, 20))
testLabel.text = test
testLabel.numberOfLines = 0
testLabel.sizeToFit()
Run Code Online (Sandbox Code Playgroud)

在目标C中

UILabel *testLabel = [[UILabel alloc] initWithFrame: CGRectMake(6, 3, 262, 20)]];
testLabel.text = test;
testLabel.numberOfLines = 0;
[testLabel sizeToFit];
Run Code Online (Sandbox Code Playgroud)


Ber*_*rik 9

如果只想在高度上调整UILabel的大小,请使用:

@property (nonatomic, weak) IBOutlet UILabel *titleLabel;

CGRect titleLabelBounds = self.titleLabel.bounds;
titleLabelBounds.size.height = CGFLOAT_MAX;
// Change limitedToNumberOfLines to your preferred limit (0 for no limit)
CGRect minimumTextRect = [self.titleLabel textRectForBounds:titleLabelBounds limitedToNumberOfLines:2];

CGFloat titleLabelHeightDelta = minimumTextRect.size.height - self.titleLabel.frame.size.height;
CGRect titleFrame = self.titleLabel.frame;
titleFrame.size.height += titleLabelHeightDelta;
self.titleLabel.frame = titleFrame;
Run Code Online (Sandbox Code Playgroud)

现在,您可以titleLabelHeightDelta根据标签大小使用布局其他视图(不使用自动布局).


Eri*_*und 6

我不确定我是否完全理解这个问题,但是您可以使用该sizeToFit方法UILabel(继承该方法UIView)根据标签文本更改大小.


Ana*_*and 6

找到号码的最简单方法.线条取决于文字.您可以使用此代码:

ceil(([aText sizeWithFont:aFont].width)/self.bounds.size.width-300); 
Run Code Online (Sandbox Code Playgroud)

它返回一些浮点值.

[lbl setNumberOfLines:floatvalue];
Run Code Online (Sandbox Code Playgroud)