【问题标题】:Adding margins to TextEditor向 TextEditor 添加边距
【发布时间】:2022-01-20 16:03:10
【问题描述】:

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

当前代码:

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

当前预览:

预期行为(页面):

不胜感激。非常感谢!

【问题讨论】:

标签: objective-c macos swiftui uitextfield swiftui-texteditor


【解决方案1】:

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

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,你不能低于它。

使用代码 (AppKit) Tab insertion inside of NSTextBlock

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    • 2015-08-14
    • 2015-06-25
    相关资源
    最近更新 更多