Skip to content

Instantly share code, notes, and snippets.

@meetAhmed
Created August 22, 2023 16:18
Show Gist options
  • Save meetAhmed/73e7e39ad8389a0f38726cc86d58b7b6 to your computer and use it in GitHub Desktop.
Save meetAhmed/73e7e39ad8389a0f38726cc86d58b7b6 to your computer and use it in GitHub Desktop.
//
// Util.swift
//
//
// Created by Ahmed Ali on 22/08/2023.
//
import Foundation
/*
html to convert:
<p><em>It is a long established fact that a reader will be distracted</em> by the readable content of a page when looking at its layout. The point of using <b>Lorem Ipsum</b> is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.</p>
*/
func applyFont() {
let mutableString = NSMutableAttributedString(attributedString: NSAttributedString(string: ""))
// this line was not working properly (italic and bold style was not applied)
// mutableString.addAttribute(.font, value: Font.FuturaPT.book.size(16), range: mutableString.range)
let newString = NSMutableAttributedString(attributedString: mutableString)
// find Font attributes and apply custom font
mutableString.enumerateAttributes(in: NSRange(0..<mutableString.length), options: .reverse) { (attributes, range, pointer) in
if let fontAttribute = attributes[NSAttributedString.Key.font] as? UIFont,
let fontFace = fontAttribute.fontDescriptor.object(forKey: .face) as? String {
let fontFaceLowercase = fontFace.lowercased()
var font: UIFont
if fontFaceLowercase.contains(Constants.Common.bold) && fontFaceLowercase.contains(Constants.Common.italic) {
font = Font.FuturaPT.demiObl.size(16)
} else if fontFaceLowercase.contains(Constants.Common.italic) {
font = Font.FuturaPT.bookOblique.size(16)
} else if fontFaceLowercase.contains(Constants.Common.bold) {
font = Font.FuturaPT.demi.size(16)
} else {
font = Font.FuturaPT.book.size(16)
}
newString.removeAttribute(NSAttributedString.Key.font, range: range)
newString.addAttribute(NSAttributedString.Key.font, value: font, range: range)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment