-
-
Notifications
You must be signed in to change notification settings - Fork 291
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,62 @@ | ||||||
// | ||||||
// Swiftfin is subject to the terms of the Mozilla Public | ||||||
// License, v2.0. If a copy of the MPL was not distributed with this | ||||||
// file, you can obtain one at https://mozilla.org/MPL/2.0/. | ||||||
// | ||||||
// Copyright (c) 2024 Jellyfin & Jellyfin Contributors | ||||||
// | ||||||
|
||||||
import SwiftUI | ||||||
|
||||||
struct HTMLFormattedText: UIViewRepresentable { | ||||||
let text: String | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
private let textView = UITextView() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Created views go within |
||||||
|
||||||
init(_ content: String) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change |
||||||
self.text = content | ||||||
} | ||||||
|
||||||
func makeUIView(context: UIViewRepresentableContext<Self>) -> UITextView { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change |
||||||
textView.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width).isActive = true | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 |
||||||
textView.isSelectable = false | ||||||
textView.isUserInteractionEnabled = false | ||||||
textView.translatesAutoresizingMaskIntoConstraints = false | ||||||
textView.isScrollEnabled = false | ||||||
return textView | ||||||
} | ||||||
|
||||||
func updateUIView(_ uiView: UITextView, context: UIViewRepresentableContext<Self>) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change |
||||||
DispatchQueue.main.async { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||||||
if let attributeText = self.converHTML(text: text) { | ||||||
textView.attributedText = attributeText | ||||||
} else { | ||||||
textView.text = "" | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
private func converHTML(text: String) -> NSAttributedString? { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||||||
guard let data = text.data(using: .utf8) else { | ||||||
return nil | ||||||
} | ||||||
|
||||||
if let attributedString = try? NSMutableAttributedString( | ||||||
data: data, | ||||||
options: [.documentType: NSAttributedString.DocumentType.html], | ||||||
documentAttributes: nil | ||||||
) { | ||||||
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 commentThe 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. |
||||||
let dynamicFont = UIFont.systemFont(ofSize: fontSize) | ||||||
let newFont = oldFont.withSize(dynamicFont.pointSize) | ||||||
attributedString.addAttributes([.font: newFont], range: range) | ||||||
} | ||||||
} | ||||||
return attributedString | ||||||
} else { | ||||||
return nil | ||||||
} | ||||||
} | ||||||
} |
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.