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

首先,我不确定我应该使用UILabel(逻辑上是显示静态文本的最佳 Select ,在我的例子中)还是UITextView.

How I wish to use it?

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

我查阅了文档和其他参考资料,但没有找到多少对我有帮助的地方,否则我可能会忽略它.

推荐答案

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];

Objective-c相关问答推荐

如何将 CGPoint 添加到 NSMutableArray?

在@implementation 而不是@interface 定义的类变量?

使用 [NSBundle mainBundle] pathForResource: ofType:inDirectory: 访问文件

在 viewDidAppear 之前无法正确设置框架

从 NSString 创建 SHA1 哈希

改变 setContentOffset:animated: 的速度?

Objective-C 方法中的静态变量是否跨实例共享?

如何在 iOS 中拨打电话?

iOS 6 Facebook 发布过程以remote_app_id 与存储的 id 不匹配结束

Lipo错误!!无法打开输入文件

在Objective-C中替换字符串中的一个字符

iOS 8 Mapkit Objc 无法获取用户位置

计算 UILabel 文本大小

Objective-C 中的 NSString indexOf

在 iOS UILabel 上设置 BOLD 字体

通过将另一个字符串重复给定次数来创建 NSString

@dynamic 在 Objective-C 中做了什么?

目标覆盖 FRAMEWORK_SEARCH_PATHS 构建设置

可以在 NSLocalizedString 中使用变量和/或参数吗?

如何在 Objective-C 中创建可变参数方法