Skip to content

Instantly share code, notes, and snippets.

@Vladlex
Created June 24, 2019 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vladlex/3c3fad98054056efecb5aa93f1217d30 to your computer and use it in GitHub Desktop.
Save Vladlex/3c3fad98054056efecb5aa93f1217d30 to your computer and use it in GitHub Desktop.
import UIKit
struct GuiUtils {
static func setAttributedStringToSize(attributedString: NSAttributedString, size: CGFloat) -> NSMutableAttributedString {
let mus = NSMutableAttributedString(attributedString: attributedString)
mus.enumerateAttribute(.font, in: NSRange(location: 0, length: mus.string.count)) { (value, range, stop) in
if let oldFont = value as? UIFont {
let newFont = oldFont.withSize(size)
mus.addAttribute(.font, value: newFont, range: range)
}
}
return mus
}
}
let initialString = NSAttributedString(string: "AA BB CC DD EE", attributes: [.font: UIFont.systemFont(ofSize: 8.0)])
let changedInitialString = initialString.mutableCopy() as! NSMutableAttributedString
changedInitialString.replaceCharacters(in: NSRange(location: 0, length: 6),
with: NSAttributedString(string: "replaced"))
let good = GuiUtils.setAttributedStringToSize(attributedString: initialString, size: 33.0)
let bad = GuiUtils.setAttributedStringToSize(attributedString: changedInitialString, size: 33.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment