环绕排除路径后 UITextView 的内容大小没有改变
Content size of UITextView not changing after wrapping around exclusion path
我有一个 UITextView
,上面有一个 UIImageView
。我有文本视图将其文本环绕在图像周围,就像这样(在视图控制器中):
override func viewDidLayoutSubviews() {
let imageBezierPath = UIBezierPath(rect: imageRect)
overviewTextView.textContainer.exclusionPaths = [imageBezierPath]
}
这可以很好地包装文本,但问题是:我在禁用滚动的 UIStackView
中有文本视图(下面的 lorem ipsum),我在它下面有项目("Label below").我希望它的高度随着它的包裹而扩大,但事实并非如此。我在设置 exclusionPaths
属性 之前和之后打印了文本视图的 contentSize.height
,但没有看到任何变化。
您可以在 lorem ipsum 的底部看到我放了一些英语以便更容易判断文本视图被截断的地方。
这是 UITextView
的预期行为吗?我该怎么做才能更新 contentSize
,或者手动计算 space 文本现在占用了多少?
更新
在@DonMag 的帮助下,我创建了一个 playground 来重现该问题。当您点击按钮切换排除路径时,文本没有按应有的方式换行。
https://gist.github.com/abbeycode/b40ade1f23a8946a12ad816e1bc0f2b1
不确定这是错误还是 "standard" 行为,或者是否有我不知道的 "force update" 命令,但是...
这是让文本视图重新计算其 contentSize 的解决方法。更改 .exclusionPaths
时,您可以清除并替换文本:
@objc
func didTap(_ sender: Any?) -> Void {
if overviewTextView.textContainer.exclusionPaths.first != nil {
overviewTextView.textContainer.exclusionPaths = []
} else {
let imageBezierPath = UIBezierPath(rect: imageRect)
overviewTextView.textContainer.exclusionPaths = [imageBezierPath]
}
let s = overviewTextView.text
overviewTextView.text = " "
overviewTextView.text = s
}
Playground 可运行完整版在这里:https://gist.github.com/DonMag/a97de9749f5915615e8a286aac5e3ec4
我有一个 UITextView
,上面有一个 UIImageView
。我有文本视图将其文本环绕在图像周围,就像这样(在视图控制器中):
override func viewDidLayoutSubviews() {
let imageBezierPath = UIBezierPath(rect: imageRect)
overviewTextView.textContainer.exclusionPaths = [imageBezierPath]
}
这可以很好地包装文本,但问题是:我在禁用滚动的 UIStackView
中有文本视图(下面的 lorem ipsum),我在它下面有项目("Label below").我希望它的高度随着它的包裹而扩大,但事实并非如此。我在设置 exclusionPaths
属性 之前和之后打印了文本视图的 contentSize.height
,但没有看到任何变化。
您可以在 lorem ipsum 的底部看到我放了一些英语以便更容易判断文本视图被截断的地方。
这是 UITextView
的预期行为吗?我该怎么做才能更新 contentSize
,或者手动计算 space 文本现在占用了多少?
更新
在@DonMag 的帮助下,我创建了一个 playground 来重现该问题。当您点击按钮切换排除路径时,文本没有按应有的方式换行。
https://gist.github.com/abbeycode/b40ade1f23a8946a12ad816e1bc0f2b1
不确定这是错误还是 "standard" 行为,或者是否有我不知道的 "force update" 命令,但是...
这是让文本视图重新计算其 contentSize 的解决方法。更改 .exclusionPaths
时,您可以清除并替换文本:
@objc
func didTap(_ sender: Any?) -> Void {
if overviewTextView.textContainer.exclusionPaths.first != nil {
overviewTextView.textContainer.exclusionPaths = []
} else {
let imageBezierPath = UIBezierPath(rect: imageRect)
overviewTextView.textContainer.exclusionPaths = [imageBezierPath]
}
let s = overviewTextView.text
overviewTextView.text = " "
overviewTextView.text = s
}
Playground 可运行完整版在这里:https://gist.github.com/DonMag/a97de9749f5915615e8a286aac5e3ec4