ios - 向 TextEditor 添加水平填充,但防止它因此在内部移动滚动条

标签 ios swift swiftui text-editor

我正在使用一个 TextEditor,我想为其添加水平填充,这样文本就不会粘在任何一个边缘上。但是,问题是如果我包含它,并且其中的文本是可滚动的,那么滚动条就不会定位到最右边,而是按填充量向内移动。

        TextEditor(text: $text)
         .background(Color.white)
         .foregroundColor(Color.black)
         .frame(maxWidth: .infinity, maxHeight: .infinity)
         .customFont(.body)
         .lineSpacing(8)
         .padding(.leading)
         .padding(.trailing)

enter image description here

最佳答案

您向外部框架添加了填充,但需要缩进内部文本容器。使用外观的可能解决方案(因为 TextEditor 实际上是 UITextView)。所以解决方案是在 TextEditor

的父 View 中添加以下内容
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/

相关文章:

ios - 将字符串参数传递给 UITapGestureRecognizer 操作

ios - UIImage 的裁剪中心

ios - Xamarin iOS : Filtering out the FileType show on Document Picker

尝试获取设备位置时 iOS 应用程序崩溃 - 线程 1 : EXC_BAD_ACCESS

swift - 更改 SKSpriteNode 的颜色属性并不会更改其颜色

ios - SwiftUI AVCapturePhotoOutput 不工作

swift - 创建一个@State var 区域,即基于两个@State var 宽度,一个@State var 高度

ios - Cocoapods 生成错误的 xcconfig 文件 - 没有 header 搜索路径

ios - 组合数组中的元素

ios - .unredacted() SwiftUI - 占位符