所以我找到了一个简单的解决方案和一个困难的解决方案。
1.简单的一个
import SwiftUI
extension NSTextView {
open override var frame: CGRect {
didSet {
// Top inset
textContainerInset = NSSize(width: 0, height: 72)
// Left fragment padding <<< This is what I was looking for
textContainer?.lineFragmentPadding = 72
}
}
}
struct TextEditingView: View {
@State private var fullText: String = "One \nTwo \nThree"
var body: some View {
TextEditor(text: $fullText)
.frame(width: 720, height: 480)
.font(.system(size: 24, design: .monospaced))
}
}
结果你得到这个:

演示库:
https ://github.com/yaosamo/Swift-TextView-Demo
2.第二种解决方案
使用 NSParagraphStyle、headIndent、firstLineHeadIndent 我相信这就是 Mac 上 Pages 上的缩进实现的方式。我不知道他们如何坚持默认缩进。如果你打开标尺,你会看到它设置为 1,你不能低于它。
在 NSTextBlock 内使用
(AppKit) Tab 插入代码
class ParagraphStyle {
let bgColor: NSColor
let paragraphStyle: NSParagraphStyle
init(bgColor: NSColor) {
self.bgColor = bgColor
//Set paragraph style
self.paragraphStyle = {
let mutableParagraphStyle = NSMutableParagraphStyle()
let specialBlock = CustomTextBlock(bgColor: bgColor)
mutableParagraphStyle.textBlocks.append(specialBlock)
mutableParagraphStyle.headIndent = 50 // Add indent here
let style = mutableParagraphStyle as NSParagraphStyle
return style
}()
}}
您可以将 headIndent 添加到文本样式。它适用于您在那里插入的副本。像我说的问题,如果你开始输入 Indents break 并且我不知道如何保存它。
第一个对我来说正是我想要的。接下来会弄清楚如何使用headIndent/FirstlineheadIndent
感谢这个社区,我能够找到解决方案!不放弃你也能做到!:D