'NSAttributedString in SwiftUI [duplicate]
everyone. I am trying to show NSAttributedString in SwiftUI. Since SwiftUI doesn't have any controls capable of it,
I am trying to use UIKit controls, wrapped in UIViewRepresentable. I've tried to use UILabel, but it doesn't handle hyperlinks.
I've tried to use UITextView (isScrollEnabled = false) but in this case text isn't broken into lines (see screenshot). When isScrollEnabled is set to true,
UITextView is displayed correctly, but I don't want any scroll.
I would be very glad to hear any suggestions.
Update: I get text from the server and I need to apply different format to different (but often intersecting!) parts of text. For example: Characters [0, 10] should be bold, characters [5, 10] should be a link, etc. I'd hate to do the parsing manually and check that no part of text is repeated. NSAttributedString has a method string.addAttribute(.font, value: font, range: NSRange(location: 0, length: 10))
Update 2: We support iOS 14+
Solution 1:[1]
Instead of relying on UIKit controls you can use a pure SwiftUI way and concatenate Text together
Group {
Text("Some text 1")
.font(font1)
.foregroundColor(color1)
+
Text("Some text 2")
.font(font2)
.foregroundColor(color2)
+
Text("Some text 3")
.font(font3)
.foregroundColor(color3)
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mag_zbc |
