更改 UITextView 的边界时,TextContainer 不会调整大小 [英] TextContainer isn't resizing while changing bounds of UITextView

查看:47
本文介绍了更改 UITextView 的边界时,TextContainer 不会调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调整textView 大小后,我希望textContainer 与textView 的大小相同(我希望文本与调整大小的textView 的宽度相同).所以我现在正在做的是我以编程方式创建一个 UITextView,然后(在 textView 中有或没有文本)我通过改变它的边界来调整 textView 的大小,但 textContainer 保持相同的大小(文本在较小的矩形中比看起来不好的 textView).

After resizing textView, I want textContainer to be the same size (I want text to be the same width as resized textView) as textView. So what I am doing right now is I create a UITextView programatically, then (with or without text inside textView) I resize textView by changing it's bounds but textContainer stays with the same size (text is in smaller rectangle than textView which looks bad).

这是一些代码:

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)

后来:

self.textView.bounds = CGRect(x, y, newWidth, newHeight)

目前我不知道为什么它不起作用并且找不到解决方案.

Currently I am out of ideas why it's not working and can't find solution.

推荐答案

把这个扔到操场上.注释掉/以不同的方式设置 textView 的大小.设置 bounds 确实不会更新容器.设置 framesize 确实如此.

Throw this in a playground. Comment out/in the different ways to set the size of the textView. Setting the bounds indeed doesn't update the container. setting the frame or size does.

import Foundation
import UIKit
import XCPlayground

var str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean feugiat purus a pharetra rhoncus. Phasellus eget velit risus. Duis varius et massa non pretium. Praesent sollicitudin egestas mi, non cursus eros ornare congue. Pellentesque ex leo, hendrerit posuere euismod ut, ultricies eleifend lectus. Phasellus eu interdum sem. Quisque ut condimentum justo. Integer non neque mauris. Vestibulum eu tellus ac nunc mollis maximus a vitae lectus. In hac habitasse platea dictumst. Sed neque sapien, elementum quis volutpat quis, dictum id ligula. Cras egestas finibus fermentum."

let textViewFrame = CGRectZero

let textStorage = NSTextStorage()

let layoutManager = NSLayoutManager()
textStorage.addLayoutManager(layoutManager)

let textContainer = NSTextContainer(size: textViewFrame.size)
layoutManager.addTextContainer(textContainer)

textContainer.widthTracksTextView = true
textContainer.heightTracksTextView = true

var textView = UITextView(frame: textViewFrame, textContainer: textContainer)
XCPShowView("MyView", view: textView)

textView.text = str

//textView.bounds = CGRect(x: 0, y: 0, width: 200, height: 200)
//textView.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
textView.frame.size = CGSize(width: 200, height: 200)

//textView.bounds = CGRect(x: 0, y: 0, width: 400, height: 200)
//textView.frame = CGRect(x: 0, y: 0, width: 400, height: 200)
textView.frame.size = CGSize(width: 400, height: 200)

<小时>

未来的读者,如果你在这里是因为你在旋转 UITextView :变换UITextView

这篇关于更改 UITextView 的边界时,TextContainer 不会调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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