调整 textView 的大小后,我希望 textContainer 与 textView 的大小相同(我希望文本与调整大小的 textView 的宽度相同)。所以我现在正在做的是我以UITextView编程方式创建一个,然后(在 textView 中有或没有文本)我通过更改它的边界来调整 textView 的大小,但 textContainer 保持相同的大小(文本位于比 textView 更小的矩形中,看起来很糟糕)。
这是一些代码:
let textStorage = NSTextStorage()
let layoutManager = NSLayoutManager()
textStorage.addLayoutManager(layoutManager)
let textContainer = NSTextContainer()
layoutManager.addTextContainer(textContainer)
textContainer.widthTracksTextView = true
textContainer.heightTracksTextView = true
self.textView = UITextView(frame: textViewFrame, textContainer: textContainer)
Run Code Online (Sandbox Code Playgroud)
然后:
self.textView.bounds = CGRect(x, y, newWidth, newHeight)
Run Code Online (Sandbox Code Playgroud)
目前我不知道为什么它不起作用并且找不到解决方案。