Reputation: 14317
I would like to change the font-size in an HTML string that I have to be half of its size.
E.g.
<div style="font-family:'Arial';font-size:43px;color:#ffffff;">
Will be
<div style="font-family:'Arial';font-size:21.5px;color:#ffffff;">
and
<div style="font-size:12px;">
Will be
<div style="font-size:6px;">
How can I do it with NSRegularExpression
?
Please note that 12 and 6 and 43 and 21.5 are only examples. I need regex since it has to be a general solution for different font-size
Upvotes: 4
Views: 629
Reputation: 8247
I would use DTCoreText for that. It parses this HTML for you and constructs an attributed string. Then you can adjust the font to your liking. Finally you can either draw the attributed string with DTCoreText, or convert it back to HTML.
If you insist on HTML, then I can offer DTHTMLParser which is a SAX-based HTML parser based on libxml2. This can parse any HTML. Though you still would have to split apart the CSS which is not as straightforward as you might think, even with RegEx. I have a category on NSString which splits the parameters so that you can reconstitute the style with modified values.
Having said that, you are probably best served by my first recommendation.
Upvotes: 1
Reputation: 539685
I am a bit reluctant to give an answer using regular expressions, because it has been stated repeatedly that parsing HTML with regex is considered harmful, impossible, dangerous to your mind, etc. And all that is correct, it is not my intention to claim anything different.
But even after all that warnings, OP has explicitly asked for a regex solution, so I am going to share this code. It can at least be useful as an example how to modify a string by looping over all matches of a regular expression.
NSString *htmlString =
@"<div style=\"font-family:'Arial';font-size:43px;color:#ffffff;\">\n"
@"<div style=\"font-size:12px;\">\n";
NSRegularExpression *regex;
regex = [NSRegularExpression regularExpressionWithPattern:@"font-size:([0-9]+)px;"
options:0
error:NULL];
NSMutableString *modifiedHtmlString = [htmlString mutableCopy];
__block int offset = 0;
[regex enumerateMatchesInString:htmlString
options:0
range:NSMakeRange(0, [htmlString length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
// range = location of the regex capture group "([0-9]+)" in htmlString:
NSRange range = [result rangeAtIndex:1];
// Adjust location for modifiedHtmlString:
range.location += offset;
// Get old point size:
NSString *oldPointSize = [modifiedHtmlString substringWithRange:range];
// Compute new point size:
NSString *newPointSize = [NSString stringWithFormat:@"%.1f", [oldPointSize floatValue]/2];
// Replace point size in modifiedHtmlString:
[modifiedHtmlString replaceCharactersInRange:range withString:newPointSize];
// Update offset:
offset += [newPointSize length] - [oldPointSize length];
}
];
NSLog(@"%@", modifiedHtmlString);
Output:
<div style="font-family:'Arial';font-size:21.5px;color:#ffffff;">
<div style="font-size:6.0px;">
Upvotes: 3
Reputation: 162712
Use a real HTML parser to preserve your sanity. An XML parser for this is incredibly fragile. There are a dozen different perfectly valid variants of the HTML syntax that will break NSAddict's expression.
I suggest reading the top voted answer on this question as it applies equally as well to HTML as it does to XHTML or XML.
RegEx match open tags except XHTML self-contained tags
Note that the iOS / OS X system frameworks include HTML/XML parsing capabilities. Use those.
Upvotes: 4
Reputation: 6852
You can do this with NSString
itself, it's pretty easy actually.
[string stringByReplacingOccurrencesOfString:@"font-size:12px;" withString:@"font-size:6px;"];
Copy this function
- (NSString *)setFontSize:(int)fontSize inHTMLString:(NSString *)htmlString {
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"font-size:[0-9]+px;" options:NSRegularExpressionCaseInsensitive error:nil];
NSString *newString = [regex stringByReplacingMatchesInString:htmlString options:NSRegularExpressionCaseInsensitive range:NSMakeRange(0, htmlString.length) withTemplate:[NSString stringWithFormat:@"font-size:%dpx;", fontSize]];
return newString;
}
Upvotes: 3