@@ -1052,16 +1052,6 @@ extension String {
1052
1052
return attributedString
1053
1053
}
1054
1054
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
-
1065
1055
public func substring( from: Int , to: Int ) -> String {
1066
1056
return self [ from..< to]
1067
1057
}
@@ -1340,17 +1330,31 @@ extension NSString {
1340
1330
1341
1331
extension String {
1342
1332
public var htmlToAttributedString : NSMutableAttributedString ? {
1343
- guard let data = data ( using: . utf8) else { return NSMutableAttributedString ( ) }
1333
+ guard let data = data ( using: . utf8) else { return nil }
1344
1334
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 )
1346
1341
return attr
1347
1342
}
1348
- catch {
1349
- return NSMutableAttributedString ( )
1350
- }
1351
1343
}
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
+ }
1354
1358
}
1355
1359
}
1356
1360
@@ -1373,3 +1377,37 @@ extension NSMutableParagraphStyle {
1373
1377
}
1374
1378
}
1375
1379
}
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
+ }
0 commit comments