我正在使用一个 TextEditor,我想为其添加水平填充,这样文本就不会粘在任何一个边缘上。但是,问题是如果我包含它,并且其中的文本是可滚动的,那么滚动条就不会定位到最右边,而是按填充量向内移动。
TextEditor(text: $text)
.background(Color.white)
.foregroundColor(Color.black)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.customFont(.body)
.lineSpacing(8)
.padding(.leading)
.padding(.trailing)
最佳答案
您向外部框架添加了填充,但需要缩进内部文本容器。使用外观的可能解决方案(因为 TextEditor 实际上是 UITextView)。所以解决方案是在 TextEditor
init() {
UITextView.appearance().textContainerInset =
UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 12) // << !!
}
// ... other code
TextEditor(text: $text)
.background(Color.white)
.foregroundColor(Color.black)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.customFont(.body)
.lineSpacing(8)
使用 Xcode 12/iOS 14 测试
关于ios - 向 TextEditor 添加水平填充,但防止它因此在内部移动滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64591082/