Search code examples
htmliosobjective-cparsingnsattributedstring

Issue with font parsing


I would really appreciate if anyone could help me to solve an issue. I need to get an attributes string from the html string I've got which contains text patterns without font style or size. I actually want to put my specific font size and style options to that piece of text.

<body>
<div>No font style <span style="font-size: 14px;"><span style="font-family: verdana, helvetica, sans-serif;">And verdana 14  <span style="font-size: 18px;">And Verdana 18</span></span></span></div>

NSAttributedString* attrStr = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUTF8StringEncoding] options:@{...}     documentAttributes:nil error:&error];

Solution

  • Please try

    NSMutableString *formattedContentString = [NSMutableString stringWithFormat:@"<style>*{font-family:%@;} !important</style> %@", @"Helvetica", htmlString];
    

    Update:

    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:contentString
    attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Your Font" size:20], NSFontAttributeName, nil]];