ios - 如何根据文本长度增加 TextView 高度

标签 ios swift

enter image description here

我想根据文本长度增加 textview 高度我尝试了一些代码,但结果像第一张图片一样,但我想像第二张图片一样。

代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    textview.text = @"This returns the size of the rectangle that fits the given string with the given font. Pass in a size with the desired width and a maximum height, and then you can look at the height returned to fit the text. There is a version that lets you specify line break mode also.This returns the size of the rectangle that fits the given string with the given font. Pass in a size with the desired width and a maximum height, and then you can look at the height returned to fit the text. There is a version that lets you specify line break mode yes.";

    CGRect frame = textview.frame;
    frame.size.height = textview.contentSize.height;
    textview.frame = frame;
    textview.ScrollEnabled = NO;
}

enter image description here

最佳答案

关于ios - 如何根据文本长度增加 TextView 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31336642/

相关文章:

ios - 如何在应用程序中显示 apns token ?在 swift 4

iOS - 导航 Controller 和自定义 View

ios - 带有 UIBezierPath 的 CAShapeLayer

swift - 是否应该将 Swift 类的实例分配给 `nil` 以查看反初始化器的运行情况?

ios - 在 UIPageViewController 中嵌入 UIScrollView 并禁用 pageView 滑动?

ios - 以编程方式创建 iOS UIStackView

ios - 在运行时更改方向更改 ViewControllers

javascript - 如何在Google Map iOS SDK上隐藏商业位置

ios - 将phonegap iOS 应用程序上传到应用程序商店无法正常工作

swift - 从自定义单元格重新加载 TableView 数据