-
-
Notifications
You must be signed in to change notification settings - Fork 290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed incorrectly formatted media descriptions. #1067
base: main
Are you sure you want to change the base?
Conversation
… changed rendering of ItemOverviewView
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I apologize but this work may be a lot more than you probably had anticipated. We "officially" support HTML and Markdown for item descriptions (and the server disclaimer).
However if you find a package that does what we need, we can use that instead.
|
||
struct HTMLFormattedText: UIViewRepresentable { | ||
let text: String | ||
private let textView = UITextView() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created views go within makeUIView
, not at the top level.
|
||
import SwiftUI | ||
|
||
struct HTMLFormattedText: UIViewRepresentable { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I asked our team and we "officially" support HTML and Markdown, so this view would have to accommodate both.
return textView | ||
} | ||
|
||
func updateUIView(_ uiView: UITextView, context: UIViewRepresentableContext<Self>) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change UIViewRepresentableContext<Self>)
to just Context
.
self.text = content | ||
} | ||
|
||
func makeUIView(context: UIViewRepresentableContext<Self>) -> UITextView { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change UIViewRepresentableContext<Self>)
to just Context
.
let range = NSRange(location: 0, length: attributedString.length) | ||
attributedString.enumerateAttribute(.font, in: range, options: []) { value, range, _ in | ||
if let oldFont = value as? UIFont { | ||
let fontSize = UIScreen.main.bounds.width * 0.05 // Adjust the multiplier as needed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't determine a font size based on the screen width. We should be using a provided system font size and the San Francisco font, not whatever serif font is being rendered.
} | ||
|
||
func updateUIView(_ uiView: UITextView, context: UIViewRepresentableContext<Self>) { | ||
DispatchQueue.main.async { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't necessary. Nothing is being updated externally that requires this view to update itself.
import SwiftUI | ||
|
||
struct HTMLFormattedText: UIViewRepresentable { | ||
let text: String |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let text: String | |
private let text: String |
let text: String | ||
private let textView = UITextView() | ||
|
||
init(_ content: String) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change content
to text
.
} | ||
|
||
func makeUIView(context: UIViewRepresentableContext<Self>) -> UITextView { | ||
textView.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width).isActive = true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
UITextView
s in SwiftUI are very tricky to get right. I actually just helped someone make a UITextView
wrapper so this came at the right time. For sizing we shouldn't set constraints and instead have to go to setContentCompressionResistancePriority
.
Here is the code that I helped the other person with, so you'll have to decipher it a bit, but it is an example of how to wrap a UITextView
in SwiftUI. I am unsure how it works with HTML or Markdown, so you will have to test that. This work may be bigger than you expected.
} | ||
} | ||
|
||
private func converHTML(text: String) -> NSAttributedString? { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will have to determine whether the text has HTML and then "render" it as HTML.
Hi if this is the case meaning html is supported how come the tags aren't being rendered correctly. Also does that mean i would have to find a package that converts html to markdown ? Because i know swift already supports markdown . |
My comment was that HTML and Markdown support was implemented, but that since we "support both", we need to implement support for both. SwiftUI sadly only supports basic inline Markdown attributes (bold, italic) but not stuff like headers or bullet points. So, technically, even Markdown needs proper implementation. We don't need something to convert HTML to Markdown, but something that supports both of them. |
I'm actually going to have to think about this a bit more. While we/other clients "officially" support HTML, I think that this would be too large of a hassle for all clients to support and would advocate for stripping HTML on the server. |
I tested the solution in this SO post ( Edit: also I noticed that |
Video descriptions often had HTML tags that weren't parsed correctly for example.
This change corrects the formatting so it now looks like