Last active
October 3, 2023 01:08
-
-
Save startupcode/fa832a15c6cb38b65f6fb4f74462b819 to your computer and use it in GitHub Desktop.
Swift - Assign HTML to NSAttributedString with custom FONT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Usage | |
lbl.attributedText = htmlToAttributedString ("html text") | |
//Assign attributed string | |
func htmlToAttributedString(string : String) -> NSAttributedString{ | |
var attribStr = NSMutableAttributedString() | |
do {//, allowLossyConversion: true | |
attribStr = try NSMutableAttributedString(data: string.dataUsingEncoding(NSUnicodeStringEncoding)!, options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil) | |
let textRangeForFont : NSRange = NSMakeRange(0, attribStr.length) | |
attribStr.addAttributes([NSFontAttributeName : UIFont(name: "Arial", size:15)!], range: textRangeForFont) | |
} catch { | |
print(error) | |
} | |
return attribStr | |
} |
Thank you . I will check and update accordingly.
Thanks for your help and improving it.
…On Sat, Jul 30, 2022 at 4:43 AM Aimer ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
The code below worked for me. First it converts html string to default
attributed string; then it enumerates font attribute in the result string,
replace the system default font with your custom font; then finally add
your new font as a new attribute.
guard let attributedString = try? NSMutableAttributedString(data: Data(htmlString.utf8),
options: [.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil)
else {
return .init(string: "Invalid content")
return
}
let range = NSRange(location: 0, length: attributedString.length)
attributedString.enumerateAttribute(.font, in: range) { value, range, pointer in
guard let currentFont = value as? UIFont else { return }
let isBold = currentFont.fontName.lowercased().contains("bold")
let replacementFont = UIFont(name: isBold ? "Arial-Bold" : "Arial", size: currentFont.pointSize)
let replacementAttribute = [NSAttributedString.Key.font: replacementFont]
attributedString.addAttributes(replacementAttribute, range: range)
}
return attributedString
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/fa832a15c6cb38b65f6fb4f74462b819#gistcomment-4249862>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADXGP54O5MRTP4G36X3ZGETVWRQSBANCNFSM5Z5K2HTA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
--
*Regards*
*---------------------------------------------------------------------------------------*
[image: facebook] <https://www.facebook.com/Vlogboard/>
[image: twitter] <https://twitter.com/myvlogboard>
[image: linkedin] <https://www.linkedin.com/in/pm-rakeshkumar>
[image: instagram] <https://www.instagram.com/vlogboard>
Rakesh Kumar
Project Manager
Information Technology | Baltech
Working Hours: 9.30 am to 7.30 pm
9950626730
***@***.***
http://www.vlogboard.com
Jaipur
The both of these gists would break markdown. For example monospace and italic text style. This could help, but not working with custom fonts https://stackoverflow.com/a/33828793/5790492
Maybe someone need only to change the size of default font https://stackoverflow.com/a/74841172/5790492
Hi
As this is old code and I'm currently not maintaining it. I will check it
and get it fixed though. Thank you for finding the bug in relation to
latest version. Appreciate it.
…On Sun, Dec 18, 2022 at 3:34 PM Nike Kov ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
The both of these gists would break markdown. For example monospace and
italic text style. This could help, but not working with custom fonts
https://stackoverflow.com/a/33828793/5790492
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/fa832a15c6cb38b65f6fb4f74462b819#gistcomment-4406541>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADXGP566WPQJO6CXB4W3UX3WN3ORFBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFQKSXMYLMOVS2I5DSOVS2I3TBNVS3W5DIOJSWCZC7OBQXE5DJMNUXAYLOORPWCY3UNF3GS5DZVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVAZTONJUGQ3TEMFHORZGSZ3HMVZKMY3SMVQXIZI>
.
You are receiving this email because you authored the thread.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
.
--
*Regards*
*---------------------------------------------------------------------------------------*
[image: facebook] <https://www.facebook.com/Vlogboard/>
[image: twitter] <https://twitter.com/myvlogboard>
[image: linkedin] <https://www.linkedin.com/in/pm-rakeshkumar>
[image: instagram] <https://www.instagram.com/vlogboard>
Rakesh Kumar
Project Manager
Information Technology | Baltech
Working Hours: 9.30 am to 7.30 pm
9950626730
***@***.***
http://www.vlogboard.com
Jaipur
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The code below worked for me.
First it converts html string to default attributed string; then it enumerates font attribute in the result string, replace the system default font with your custom font; then finally add your new font as a new attribute.