gpt4 book ai didi

ios - 将 UITextView 属性字符串保存到 SwiftUI 中的文件

转载 作者:行者123 更新时间:2023-12-04 08:13:22 28 4
gpt4 key购买 nike

我需要使用 NSMutableAttributedString 中的属性字符串 ( SwiftUI )制作一个简单的富文本编辑器,您可能已经知道,SwiftUI 本身不支持属性字符串。所以我不得不使用旧的 UITextView使用 UIViewRepresentable包装。
现在,我的应用程序是基于文档的应用程序,每当我尝试保存文件时,都会发生一些奇怪的问题:
第一个问题:当我运行应用程序并打开文件并开始输入时,文件的初始内容将被删除。
第二个问题:每当我写文本并点击后退箭头保存文件时,它永远不会更新。所有文档仍然具有相同的初始内容。

文档处理代码是您创建新的基于 SwiftUI 文档的应用程序时的默认代码,但是,我将编码从纯文本更改为 NSMutableAttributedString . (我还创建了一个名为 .mxt 而不是 .txt 的文档扩展名)
文件处理文件MyextDocument.swift :

import SwiftUI
import UniformTypeIdentifiers

extension UTType {
static var MyextDocument = UTType(exportedAs: "com.example.Myext.mxt")
}

struct MyextDocument: FileDocument {
var text: NSMutableAttributedString

init(text: NSMutableAttributedString = NSMutableAttributedString()) {
self.text = text
}

static var readableContentTypes: [UTType] { [.MyextDocument] }

init(configuration: ReadConfiguration) throws {
guard let data = configuration.file.regularFileContents,
let string = try? NSMutableAttributedString(data: data, options: [NSMutableAttributedString.DocumentReadingOptionKey.documentType : NSMutableAttributedString.DocumentType.rtf], documentAttributes: nil)
else {
throw CocoaError(.fileReadCorruptFile)
}
text = string
}

func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
let data = (try? text.data(from: NSMakeRange(0, text.length), documentAttributes: [.documentType: NSMutableAttributedString.DocumentType.rtf]))!
return .init(regularFileWithContents: data)
}
}
UIViewRepresentable 包装文件 iOSEditorTextView.swift :
import Combine
import SwiftUI
import UIKit

struct iOSEditorTextView: UIViewRepresentable {
//@Binding var text: String
@Binding var document: NSMutableAttributedString
var isEditable: Bool = true
var font: UIFont? = .systemFont(ofSize: 14, weight: .regular)

var onEditingChanged: () -> Void = {}
var onCommit : () -> Void = {}
var onTextChange : (String) -> Void = { _ in }

func makeCoordinator() -> Coordinator {
Coordinator(self)
}

func makeUIView(context: Context) -> CustomTextView {
let textView = CustomTextView(
text: document,
isEditable: isEditable,
font: font
)
textView.delegate = context.coordinator

return textView
}

func updateUIView(_ uiView: CustomTextView, context: Context) {
uiView.text = document
uiView.selectedRanges = context.coordinator.selectedRanges
}
}

// MARK: - Preview

#if DEBUG

struct iOSEditorTextView_Previews: PreviewProvider {
static var previews: some View {
Group {
iOSEditorTextView(
document: .constant(NSMutableAttributedString()),
isEditable: true,
font: .systemFont(ofSize: 14, weight: .regular)
)
.environment(\.colorScheme, .dark)
.previewDisplayName("Dark Mode")

iOSEditorTextView(
document: .constant(NSMutableAttributedString()),
isEditable: false
)
.environment(\.colorScheme, .light)
.previewDisplayName("Light Mode")
}
}
}

#endif

// MARK: - Coordinator

extension iOSEditorTextView {

class Coordinator: NSObject, UITextViewDelegate {
var parent: iOSEditorTextView
var selectedRanges: [NSValue] = []

init(_ parent: iOSEditorTextView) {
self.parent = parent
}

func textViewDidBeginEditing(_ textView: UITextView) {
self.parent.document = textView.attributedText as! NSMutableAttributedString
self.parent.onEditingChanged()
}

func textViewDidChange(_ textView: UITextView) {
self.parent.document = textView.attributedText as! NSMutableAttributedString
//self.selectedRanges = textView.selectedRange
}

func textViewDidEndEditing(_ textView: UITextView) {
self.parent.document = textView.attributedText as! NSMutableAttributedString
self.parent.onCommit()
}
}
}

// MARK: - CustomTextView

final class CustomTextView: UIView, UIGestureRecognizerDelegate, UITextViewDelegate {
private var isEditable: Bool
private var font: UIFont?

weak var delegate: UITextViewDelegate?

var text: NSMutableAttributedString {
didSet {
textView.attributedText = text
}
}

var selectedRanges: [NSValue] = [] {
didSet {
guard selectedRanges.count > 0 else {
return
}

//textView.selectedRanges = selectedRanges
}
}

private lazy var textView: UITextView = {
let textView = UITextView(frame: .zero)
textView.delegate = self.delegate
textView.font = self.font
textView.isEditable = self.isEditable
textView.textColor = UIColor.label
textView.textContainerInset = UIEdgeInsets(top: 40, left: 0, bottom: 0, right: 0)
textView.translatesAutoresizingMaskIntoConstraints = false

return textView
}()

// Create paragraph styles
let paragraphStyle = NSMutableParagraphStyle() // create paragraph style

var attributes: [NSMutableAttributedString.Key: Any] = [
.foregroundColor: UIColor.red,
.font: UIFont(name: "Courier", size: 12)!
]

// MARK: - Init
init(text: NSMutableAttributedString, isEditable: Bool, font: UIFont?) {
self.font = font
self.isEditable = isEditable
self.text = text

super.init(frame: .zero)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

// MARK: - Life cycle

override func draw(_ rect: CGRect) {
super.draw(rect)

setupTextView()

// Set tap gesture
let tap = UITapGestureRecognizer(target: self, action: #selector(didTapTextView(_:)))
tap.delegate = self
textView.addGestureRecognizer(tap)

// create paragraph style
self.paragraphStyle.headIndent = 108

// create attributed string
let string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

// create attributes
self.attributes = [
.foregroundColor: UIColor.red,
.font: UIFont(name: "Courier", size: 12)!,
.paragraphStyle: paragraphStyle,
]

// Create the Attributed String
let myAttrString = NSMutableAttributedString(string: string, attributes: attributes)

// Write it to the Text View
textView.attributedText = myAttrString
}

// Show cursor and set it to position on tapping + Detect line
@objc func didTapTextView(_ recognizer: UITapGestureRecognizer) {
// Show cursor and set it to position on tapping
if recognizer.state == .ended {
textView.isEditable = true
textView.becomeFirstResponder()

let location = recognizer.location(in: textView)
if let position = textView.closestPosition(to: location) {
let uiTextRange = textView.textRange(from: position, to: position)

if let start = uiTextRange?.start, let end = uiTextRange?.end {
let loc = textView.offset(from: textView.beginningOfDocument, to: position)
let length = textView.offset(from: start, to: end)

textView.selectedRange = NSMakeRange(loc, length)
}
}

}

}

func setupTextView() {
// Setup Text View delegate
textView.delegate = self

// Place the Text View on the view
addSubview(textView)

NSLayoutConstraint.activate([
textView.topAnchor.constraint(equalTo: topAnchor),
textView.trailingAnchor.constraint(equalTo: trailingAnchor),
textView.leadingAnchor.constraint(equalTo: leadingAnchor),
textView.bottomAnchor.constraint(equalTo: bottomAnchor)
])

}
}
为了调用 UIViewRepresentable 包装器,我在 ContentView 中编写了以下代码:
iOSEditorTextView(
document: $document.text,
isEditable: true,
font: .systemFont(ofSize: 14, weight: .regular)
)
任何帮助将不胜感激。

最佳答案

代码中有几个错误。

  • 您设置了错误的委托(delegate)。所以你的委托(delegate)方法不起作用。
    给自我委托(delegate),而不是自我。喜欢
  • func setupTextView() {
    // Setup Text View delegate
    textView.delegate = delegate
  • 使用NSMutableAttributedString在代表中。
  • func textViewDidBeginEditing(_ textView: UITextView) {
    self.parent.document = NSMutableAttributedString(attributedString: textView.attributedText)
    self.parent.onEditingChanged()
    }

    func textViewDidChange(_ textView: UITextView) {
    self.parent.document = NSMutableAttributedString(attributedString: textView.attributedText)
    }

    func textViewDidEndEditing(_ textView: UITextView) {
    self.parent.document = NSMutableAttributedString(attributedString: textView.attributedText)
    self.parent.onCommit()
    }
  • override func draw(_ rect: CGRect) 中删除静态文本此行覆盖现有文本。
  • override func draw(_ rect: CGRect) {
    super.draw(rect)

    setupTextView()

    // Set tap gesture
    let tap = UITapGestureRecognizer(target: self, action: #selector(didTapTextView(_:)))
    tap.delegate = self
    textView.addGestureRecognizer(tap)

    // create paragraph style
    self.paragraphStyle.headIndent = 108

    // create attributes
    self.attributes = [
    .foregroundColor: UIColor.red,
    .font: UIFont(name: "Courier", size: 12)!,
    .paragraphStyle: paragraphStyle,
    ]
    }
    注意:从draw rect中删除其他代码并使用init或 func awakeFromNib()

    关于ios - 将 UITextView 属性字符串保存到 SwiftUI 中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65839021/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com