Skip to content

Commit 24ed766

Browse files
author
แ„‡แ…กแ†จแ„€แ…ตแ†ฏแ„’แ…ฉ
committed
UILabel setHTMLText ์ถ”๊ฐ€
UILabel setHTMLText ์ถ”๊ฐ€
1 parent b785b5f commit 24ed766

File tree

3 files changed

+81
-19
lines changed

3 files changed

+81
-19
lines changed

โ€ŽSources/SwiftHelper/Data+Extention/StringExtensions.swift

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,16 +1052,6 @@ extension String {
10521052
return attributedString
10531053
}
10541054

1055-
public func convertHtml() -> NSAttributedString {
1056-
guard let data = data(using: .unicode) else { return NSAttributedString() }
1057-
do {
1058-
return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
1059-
}
1060-
catch {
1061-
return NSAttributedString()
1062-
}
1063-
}
1064-
10651055
public func substring(from: Int, to: Int) -> String {
10661056
return self[from..<to]
10671057
}
@@ -1340,17 +1330,31 @@ extension NSString {
13401330

13411331
extension String {
13421332
public var htmlToAttributedString: NSMutableAttributedString? {
1343-
guard let data = data(using: .utf8) else { return NSMutableAttributedString() }
1333+
guard let data = data(using: .utf8) else { return nil }
13441334
do {
1345-
let attr = try NSMutableAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
1335+
let attr = try? NSMutableAttributedString(data: data,
1336+
options: [
1337+
.documentType: NSAttributedString.DocumentType.html,
1338+
.characterEncoding: String.Encoding.utf8.rawValue
1339+
],
1340+
documentAttributes: nil)
13461341
return attr
13471342
}
1348-
catch {
1349-
return NSMutableAttributedString()
1350-
}
13511343
}
1352-
var htmlToString: String {
1353-
return htmlToAttributedString?.string ?? ""
1344+
// HTML ํƒœ๊ทธ ์ œ๊ฑฐ ํ•จ์ˆ˜ (๋ณ€ํ™˜ ์‹คํŒจ ์‹œ ๋Œ€์ฒด์šฉ)
1345+
public func stripHTMLTags() -> String {
1346+
return self.replacingOccurrences(of: "<[^>]+>", with: "",
1347+
options: .regularExpression,
1348+
range: nil)
1349+
}
1350+
1351+
public func htmlToAttributedStringWithCustomFont(_ font: UIFont) -> NSAttributedString? {
1352+
if let attributedText = self.replace("\\n", "\n").replace("\n", "<br>").htmlToAttributedString {
1353+
return attributedText.applyCustomFont(font)
1354+
}
1355+
else {
1356+
return NSAttributedString(string: self.stripHTMLTags())
1357+
}
13541358
}
13551359
}
13561360

@@ -1373,3 +1377,37 @@ extension NSMutableParagraphStyle {
13731377
}
13741378
}
13751379
}
1380+
1381+
extension NSMutableAttributedString {
1382+
// HTML ์•ˆ์ „ ๊ฒ€์‚ฌ ํ•จ์ˆ˜ (ํ•„์š”์— ๋”ฐ๋ผ ๊ตฌํ˜„)
1383+
public func applyCustomFont(_ font: UIFont) -> Self {
1384+
let fullRange = NSRange(location: 0, length: self.length)
1385+
self.enumerateAttribute(.font, in: fullRange) { (value, range, _) in
1386+
if let originalFont = value as? UIFont {
1387+
let traits = originalFont.fontDescriptor.symbolicTraits
1388+
var newFont = font
1389+
1390+
// ๋ณผ๋“œ ์Šคํƒ€์ผ ์œ ์ง€
1391+
if traits.contains(.traitBold) {
1392+
newFont = UIFont.boldSystemFont(ofSize: font.pointSize)
1393+
}
1394+
1395+
// ์ดํƒค๋ฆญ ์Šคํƒ€์ผ ์œ ์ง€
1396+
if traits.contains(.traitItalic) {
1397+
newFont = UIFont.italicSystemFont(ofSize: font.pointSize)
1398+
}
1399+
1400+
self.addAttribute(.font, value: newFont, range: range)
1401+
}
1402+
}
1403+
return self
1404+
}
1405+
1406+
public func applyCustomColor(_ color: UIColor) -> Self {
1407+
let fullRange = NSRange(location: 0, length: self.length)
1408+
self.enumerateAttribute(.foregroundColor, in: fullRange) { (_, range, _) in
1409+
self.addAttribute(.font, value: color, range: range)
1410+
}
1411+
return self
1412+
}
1413+
}

โ€ŽSources/SwiftHelper/UI+Extention/UILabelExtensions.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,28 @@ extension UILabel {
166166
}
167167
}
168168

169+
// HTML
170+
extension UILabel {
171+
public func setHTMLText(_ text: String?, lineBreakMode: NSLineBreakMode = .byTruncatingTail) {
172+
self.text = nil
173+
self.attributedText = nil
174+
guard let text else { return }
175+
if let attributedText = text.replace("\\n", "\n").replace("\n", "<br>").htmlToAttributedString {
176+
self.attributedText = attributedText.applyCustomFont(self.font)
177+
}
178+
else {
179+
self.attributedText = NSAttributedString(string: text.stripHTMLTags())
180+
}
181+
182+
// ์Šคํƒ€์ผ์„ ์œ ์ง€ํ•˜๋ฉด์„œ ํ•„์š”ํ•œ ์†์„ฑ ์ถ”๊ฐ€ํ•˜๊ธฐ
183+
if let mutableAttr = attributedText?.mutableCopy() as? NSMutableAttributedString {
184+
let range = NSRange(location: 0, length: mutableAttr.length)
185+
let paragraphStyle = NSMutableParagraphStyle()
186+
paragraphStyle.lineBreakMode = lineBreakMode
187+
mutableAttr.addAttribute(.paragraphStyle, value: paragraphStyle, range: range)
188+
attributedText = mutableAttr
189+
}
190+
}
191+
}
192+
169193
#endif

โ€ŽSources/SwiftHelper/View/ToastView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ public final class ToastView: UIView {
171171
}
172172
// ํ•œ์ค„์ธ์ง€ ๊ฒ€์‚ฌ(1์ค„:1.5์ดˆ 2์ค„์ด์ƒ:3์ดˆ)
173173
let delay = self.textLabel.frame.size.height > TextLineOneSize ? 3 : 1.5
174-
Self.timer = Timer.schedule(delay: delay) { [weak self] timer in
174+
Self.timer = Timer.schedule(repeatInterval: delay) { [weak self] (timer, _) in
175175
guard let self else { return }
176-
timer?.invalidate()
176+
timer.invalidate()
177177
Self.timer = nil
178178
self.fadeOut()
179179
}

0 commit comments

Comments
ย (0)