ResizingTextView

这是一个适用于 iOS 和 macOS 的 SwiftUI 可调整大小的文本视图。

用法

您可以使用示例项目来查看它的工作方式。 这些是来自该项目的示例代码

// Self-sizing automatically (Default)
ResizingTextView(text: $text1)

// Fixed height, scrollable, newline characters not allowed
ResizingTextView(
    text: $text2,
    placeholder: "Placeholder",
    isScrollable: true,
    canHaveNewLineCharacters: false
)
.frame(height: 80)

// Uneditable, selectable, color/font changed
ResizingTextView(
    text: $text3,
    isEditable: false
)
.font(.boldSystemFont(ofSize: 16))
.foregroundColor(.magenta)

// Uneditable, selectable, max 2 lines
ResizingTextView(
    text: .constant("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."),
    isEditable: false,
    lineLimit: 2
)

// Uneditable, unselectable, max 2 lines
ResizingTextView(
    text: .constant("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."),
    isEditable: false,
    isSelectable: false,
    lineLimit: 2
)

// Selectable, uneditable, non greedy
ResizingTextView(
    text: .constant("Lorem ipsum"),
    isEditable: false,
    hasGreedyWidth: false
)
.background(.yellow)

#if os(iOS)
// No autocapitalization (iOS Only)
ResizingTextView(
    text: $text4,
    placeholder: "Placeholder"
)
.autocapitalizationType(.none)
#endif

// Customized textContentInset
ResizingTextView(
    text: $text5
)
#if os(macOS)
.textContainerInset(CGSize(width: 40, height: 10))
#elseif os(iOS)
.textContainerInset(UIEdgeInsets(top: 0, left: 40, bottom: 0, right: 40))
#endif
}

演示

iOS

Screen.Recording.2022-08-14.at.6.35.55.pm.mov

macOS

Screen.Recording.2022-08-14.at.6.33.57.pm.mov

使用此软件包的应用