当通过attributedText值添加的lineSpacing更改时,TextView UIViewRepresent



我使用的是TextView UIViewRepresentable,如下所示https://www.appcoda.com/swiftui-textview-uiviewrepresentable/.

除了一个关于行距的问题外,它的工作与预期的一样。SwiftUI的lineSpacing修饰符似乎对它没有影响。因此,我通过在UIViewRepresentable的func updateUIView(_ uiView: UITextView, context: Context):

let style = NSMutableParagraphStyle()
style.lineSpacing = 4
let attributes = [NSAttributedString.Key.paragraphStyle : style]
uiView.attributedText = NSAttributedString(string: self.text, attributes:attributes)

这是通过调整UITextView的行距来建议的。

因此,完整的功能看起来像:

func updateUIView(_ uiView: UITextView, context: Context) {
uiView.text = text

// line spacing
let style = NSMutableParagraphStyle()
style.lineSpacing = 4
let attributes = [NSAttributedString.Key.paragraphStyle : style]
uiView.attributedText = NSAttributedString(string: self.text, attributes:attributes)
}

这会完成任务,但会导致UndoManager重置。也就是说,一旦我做出任何更改,UndoManager就不认为有任何事情可以撤消(或重做(。从我的搜索中,这似乎是更改attributedText值的一般副作用。我想知道是否有一个变通方法,是对我的方法进行调整,还是在不重置UndoManager状态的情况下以完全不同的方式实现lineSpacing

更新:尝试了Asperi的推荐,但结果喜忧参半。

这是TextView和相应协调器的完整代码:

import SwiftUI

struct TextView: UIViewRepresentable {

// MARK: Bindings
@Binding var text: String
@Binding var textStyle: UIFont.TextStyle
// MARK: -
// MARK: Functions
func makeUIView(context: Context) -> UITextView {
let textView = UITextView()
textView.backgroundColor = UIColor.clear
textView.delegate = context.coordinator
textView.autocapitalizationType = .sentences
textView.isSelectable = true
textView.isUserInteractionEnabled = true
textView.adjustsFontForContentSizeCategory = true

return textView
}
func makeCoordinator() -> Coordinator {
Coordinator($text)
}
func updateUIView(_ uiView: UITextView, context: Context) {
let storage = uiView.textStorage
storage.beginEditing()
// line spacing
let style = NSMutableParagraphStyle()
style.lineSpacing = 4
let attributes = [NSAttributedString.Key.paragraphStyle : style]
storage.replaceCharacters(in: NSRange(location: 0, length: storage.length),
with: NSAttributedString(string: self.text, attributes:attributes))
storage.endEditing()
}
// MARK: -
// MARK: Internal classes
class Coordinator: NSObject, UITextViewDelegate {

// MARK: Local
var text: Binding<String>

// MARK: -
init(_ text: Binding<String>) {
self.text = text
}

// MARK: -
// MARK: Functions
func textViewDidChange(_ textView: UITextView) {
self.text.wrappedValue = textView.text
}
}
}

如果我在textViewDidChange中保留self.text.wrappedValue=textView.text(它取自顶部链接的AppCoda教程(,那么推荐就不起作用。然而,如果我删除它,它似乎可以工作,但还有其他问题,即每当刷新视图(我认为(时,文本都会在会话开始时自动重置为原始状态——例如,如果我尝试切换到另一个应用程序,我可以在切换之前看到文本重置,或者当我打开一个UI时,例如,降低TextView的不透明度。

由于Ivan在https://stackoverflow.com/a/44414510/698971.

在文本视图UIViewRepresentable的func makeUIView(context: Context) -> UITextView内部,我需要添加:

let spacing = NSMutableParagraphStyle()
spacing.lineSpacing = 4
let attr = [NSAttributedString.Key.paragraphStyle : spacing]
textView.typingAttributes = attr

最新更新