Skip to content

Commit

Permalink
Merge pull request #435 from SourcePointUSA/DIA-1831-fix-html-tags-v7
Browse files Browse the repository at this point in the history
[DIA-1831] HTML tags for `Description` text on `HomeView` are applied properly
  • Loading branch information
andresilveirah committed May 2, 2023
2 parents a8a9ae8 + d38c9fe commit d7fb472
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
13 changes: 8 additions & 5 deletions ConsentViewController/Classes/Extensions/SPString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import Foundation

extension String {
var htmlToAttributedString: NSAttributedString? {
guard let data = self.stripOutHtml()?.data(using: .utf8) else { return nil }
guard let data = self.stripOutCss()?.data(using: .utf8) else { return nil }
do {
return try NSAttributedString(
data: data,
options: [
.documentType: NSAttributedString.DocumentType.plain,
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue
],
documentAttributes: nil
Expand All @@ -24,13 +24,16 @@ extension String {
}
}
var htmlToString: String { htmlToAttributedString?.string ?? "" }
func stripOutHtml() -> String? {
func stripOutCss(stripHtml: Bool = false) -> String? {
// replace &lt; with < and &gt; with >
let decoded = self.replacingOccurrences(of: "&lt;", with: "<").replacingOccurrences(of: "&gt;", with: ">")
// removes any CSS inline styling
let noCSS = decoded.replacingOccurrences(of: "<style>[^>]+</style>", with: "", options: .regularExpression, range: nil)
// removes any HTML tags
let noHTML = noCSS.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil)
return noHTML
var result = noCSS
if(stripHtml){
result = result.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil)
}
return result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class FocusGuideDebugView: UIView {
if let text = text {
textView.attributedText = text.htmlToAttributedString
} else {
textView.text = textViewComponent.settings.text.stripOutHtml()
textView.attributedText = textViewComponent.settings.text.htmlToAttributedString
}
textView.textColor = UIColor(hexString: textViewComponent.settings.style?.font?.color)
textView.isUserInteractionEnabled = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ extension SPGDPRPartnersViewController: UITableViewDataSource, UITableViewDelega

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 50))
label.text = "\(sections[section]?.settings.text.stripOutHtml() ?? "Partners")"
label.text = "\(sections[section]?.settings.text.stripOutCss(stripHtml: true) ?? "Partners")"
label.font = UIFont(from: sections[section]?.settings.style?.font)
label.textColor = UIColor(hexString: sections[section]?.settings.style?.font?.color)
return label
Expand Down

0 comments on commit d7fb472

Please sign in to comment.