Created
June 24, 2019 13:19
-
-
Save Vladlex/3c3fad98054056efecb5aa93f1217d30 to your computer and use it in GitHub Desktop.
This file contains 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
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