3

我正在向 TextEditor 添加边距。同时将这些边距保持为可点击区域。我能够添加textContainerInset并且问题是添加的 Inset 不可点击。

当前代码:

extension NSTextView {
  open override var frame: CGRect {
    didSet {
        textContainerInset = CGSize(width: 72, height: 72)
      }
   }
}

当前预览:

预期行为(页面):

将不胜感激。非常感谢!

4

1 回答 1

2

所以我找到了一个简单的解决方案和一个困难的解决方案。

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

于 2021-12-21T18:01:19.447 回答