UITextView 和 NSAttributedString 未在 SwiftUI 中显示
UITextView and NSAttributedString is not showing in SwiftUI
我正在尝试通过 UIViewRepresentable 使用 UITextView 和 NSAttributedString。
这是我的代码。
- AttrText 是包含文本 (String) 以及有关文本颜色和字体的信息的结构。
struct TextView: View {
let attrTextList: [AttrText]
let size: CGFloat
let lineSpacing: CGFloat
var body: some View {
IntegratedTextView(attrTextList: attrTextList, alignment: .left, size: size)
.frame(minWidth: 0, maxWidth: .infinity)
}
}
struct IntegratedTextView: UIViewRepresentable {
let attrTextList: [AttrText]
let alignment: NSTextAlignment
let size: CGFloat
init(attrTextList: [AttrText], alignment: NSTextAlignment, size: CGFloat) {
self.attrTextList = attrTextList
self.alignment = alignment
self.size = size
}
func makeUIView(context: Context) -> UITextView {
let textView = UITextView()
let result = attrTextList.map { text -> NSAttributedString in
var attrString = NSMutableAttributedString(string: text.text)
let attribute: [NSAttributedString.Key: Any] = [
NSAttributedString.Key.font: processFont(text: text, size: size),
NSAttributedString.Key.foregroundColor: processColor(text: text)
]
attrString.addAttributes(attribute, range: NSRange(location: 0, length: attrString.length))
return attrString
}
.reduce(NSAttributedString(string: ""), +)
textView.attributedText = result
textView.textAlignment = alignment
print("TextView: \(textView)")
return textView
}
func updateUIView(_ textView: UITextView, context: Context) {
}
func processColor(text: AttrText) -> UIColor {
//something return color by AttrText
}
func processFont(text: AttrText, size: CGFloat) -> UIFont {
//something return font by AttrText and size
}
}
extension NSAttributedString {
static func +(left: NSAttributedString, right: NSAttributedString) -> NSAttributedString {
let result = NSMutableAttributedString()
result.append(left)
result.append(right)
return result
}
}
我认为它应该有效,但事实并非如此。当我打开应用程序时,IntegratedTextView 是不可见的。
通过 print("TextView: \(textView)")
,我检查了控制台日志,它打印如下。
TextView: <UITextView: 0x1351c3600; frame = (0 0; 0 0); text = 'It's Text'; clipsToBounds = YES; gestureRecognizers = <NSArray: 0x280682700>; layer = <CALayer: 0x2808e5700>; contentOffset: {0, 0}; contentSize: {0, 40}; adjustedContentInset: {0, 0, 0, 0}>
有没有人如何解决这个问题?
这个问题是由ScrollView调用的。
我的 TextView() 在较高视图中被 ScrollView 包裹。
您可以在 link ->
中找到答案
我正在尝试通过 UIViewRepresentable 使用 UITextView 和 NSAttributedString。 这是我的代码。
- AttrText 是包含文本 (String) 以及有关文本颜色和字体的信息的结构。
struct TextView: View {
let attrTextList: [AttrText]
let size: CGFloat
let lineSpacing: CGFloat
var body: some View {
IntegratedTextView(attrTextList: attrTextList, alignment: .left, size: size)
.frame(minWidth: 0, maxWidth: .infinity)
}
}
struct IntegratedTextView: UIViewRepresentable {
let attrTextList: [AttrText]
let alignment: NSTextAlignment
let size: CGFloat
init(attrTextList: [AttrText], alignment: NSTextAlignment, size: CGFloat) {
self.attrTextList = attrTextList
self.alignment = alignment
self.size = size
}
func makeUIView(context: Context) -> UITextView {
let textView = UITextView()
let result = attrTextList.map { text -> NSAttributedString in
var attrString = NSMutableAttributedString(string: text.text)
let attribute: [NSAttributedString.Key: Any] = [
NSAttributedString.Key.font: processFont(text: text, size: size),
NSAttributedString.Key.foregroundColor: processColor(text: text)
]
attrString.addAttributes(attribute, range: NSRange(location: 0, length: attrString.length))
return attrString
}
.reduce(NSAttributedString(string: ""), +)
textView.attributedText = result
textView.textAlignment = alignment
print("TextView: \(textView)")
return textView
}
func updateUIView(_ textView: UITextView, context: Context) {
}
func processColor(text: AttrText) -> UIColor {
//something return color by AttrText
}
func processFont(text: AttrText, size: CGFloat) -> UIFont {
//something return font by AttrText and size
}
}
extension NSAttributedString {
static func +(left: NSAttributedString, right: NSAttributedString) -> NSAttributedString {
let result = NSMutableAttributedString()
result.append(left)
result.append(right)
return result
}
}
我认为它应该有效,但事实并非如此。当我打开应用程序时,IntegratedTextView 是不可见的。
通过 print("TextView: \(textView)")
,我检查了控制台日志,它打印如下。
TextView: <UITextView: 0x1351c3600; frame = (0 0; 0 0); text = 'It's Text'; clipsToBounds = YES; gestureRecognizers = <NSArray: 0x280682700>; layer = <CALayer: 0x2808e5700>; contentOffset: {0, 0}; contentSize: {0, 40}; adjustedContentInset: {0, 0, 0, 0}>
有没有人如何解决这个问题?
这个问题是由ScrollView调用的。
我的 TextView() 在较高视图中被 ScrollView 包裹。
您可以在 link ->