NSAttributedString change font size
With these extensions you will be able to easily change font size of the NSAttributedString
in all of the subranges leaving other font parameters the same.
Usage
let label: UILabel = ...
let string: NSAttributedString = ...
label.attributedText = string.mutable.setFontSize(20)
Extensions
extension NSMutableAttributedString {
func setFontSize(_ fontSize: CGFloat) {
beginEditing()
enumerateAttribute(.font, in: completeRange) { value, range, _ in
guard
let fontFromAttribute = value as? UIFont,
let descriptor = fontFromAttribute.fontDescriptor
.withSymbolicTraits(fontFromAttribute.fontDescriptor.symbolicTraits)
else { return }
let font = UIFont(descriptor: descriptor, size: fontSize)
addAttribute(.font, value: font, range: range)
}
endEditing()
}
}
extension NSAttributedString {
var mutable: NSMutableAttributedString {
NSMutableAttributedString(attributedString: self)
}
var completeRange: NSRange {
NSRange(location: 0, length: self.length)
}
}
iOS 7 using an HTML string as an NSAttributedString AND setting the font?
The way I was able to get it to work was to prepend my string with <span style=\"font-family: Avenir; font-size: 12\">%@</span>
Parsing HTML into NSAttributedText - how to set font?
Figured it out. Bit of a bear, and maybe not the best answer.
This code will go through all the font changes. I know that it is using "Times New Roman" and "Times New Roman BoldMT" for the fonts.
But regardless, this will find the bold fonts and let me reset them. I can also reset the size while I'm at it.
I honestly hope/think there is a way to set this up at parse time, but I can't find it if there is.
NSRange range = (NSRange){0,[str length]};
[str enumerateAttribute:NSFontAttributeName inRange:range options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(id value, NSRange range, BOOL *stop) {
UIFont* currentFont = value;
UIFont *replacementFont = nil;
if ([currentFont.fontName rangeOfString:@"bold" options:NSCaseInsensitiveSearch].location != NSNotFound) {
replacementFont = [UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:25.0f];
} else {
replacementFont = [UIFont fontWithName:@"HelveticaNeue-Thin" size:25.0f];
}
[str addAttribute:NSFontAttributeName value:replacementFont range:range];
}];
Swift - Font size increases when converting html string to attributed string and back again
Try this method I found from some help and then converted to Swift 3:
func newAttrSize(blockQuote: NSAttributedString) -> NSAttributedString
{
let yourAttrStr = NSMutableAttributedString(attributedString: blockQuote)
yourAttrStr.enumerateAttribute(.font, in: NSMakeRange(0, yourAttrStr.length), options: .init(rawValue: 0)) {
(value, range, stop) in
if let font = value as? UIFont {
let resizedFont = font.withSize(font.pointSize * 0.75)
yourAttrStr.addAttribute(.font, value: resizedFont, range: range)
}
}
return yourAttrStr
}
Related Topics
Get Input Type=Text to Look Like Type=Password
How to Create a Slanted Background with CSS
Flexbox Resize and Scrollable Overflow
Html/CSS Hack to Allow Overflow-X: Auto & Overflow-Y: Visible
Why Do Overflow Clear Margin of P-Tag
CSS Remove Default Blue Border
Removing Border Style Changes Page Layout
How to Make an Input Element Occupy All Remaining Horizontal Space
How to Change an Image on Click Using CSS Alone
Grails: Displaying Created Image in Gsp
Why Is There Vertical Scroll on the Main Container
Flexbox Layout Does Not Work in Internet Explorer 10
What CSS Should I Use to Get a Border Around an Option Tag in Both Firefox and Ie