Ios7 Font Size Change When Create Nsattributedstring from HTML

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



Leave a reply



Submit