NSString: cut <b> </b> and make an attribute string with color for this segment?

Tell me if there is a line:

This is <b> simple </b>.

I need to get rid of <b>, (sorry there is no space between b and the angle bracket, for some reason the preview does not show it), also make the word β€œjust” bold, my thought was:

  • replace angle brackets and br with empty space
  • make a β€œsimple” segment attributes

The problem is that after removing the tags, I still need to know the location of the word, I first remember the location of the β€œsimple”, after removing the location-4 should be the new location of the β€œsimple”? Is there a better way? Or even convert the html tag to attributes?

thanks

edit: Must be b instead of br

+6
source share
2 answers

IOS 7 has an API that makes this very simple. It converts the NSString (possible) HTML text to NSAttributedString .

 NSDictionary *options = @{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType }; NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithData:[myHTMLString dataUsingEncoding:NSUTF8StringEncoding] options:options documentAttributes:nil error:nil]; 

It will even retain any inline CSS, even background-color !

Please note that if the font and font size are not specified in the HTML text, the default will be Times New Roman 12. You can specify it as follows: <style>body { font-family:-apple-system; font-size:14px; }<style> <style>body { font-family:-apple-system; font-size:14px; }<style> <style>body { font-family:-apple-system; font-size:14px; }<style> . If you do not specify the font using CSS, you can still override the font, but you will have to manually handle the bold font, italics, etc., Otherwise, the formatting will be lost if you set the font for the entire line. One approach is enumerateAttribute: NSFontAttributeName in a mutable attribute string that looks for bold, etc. In the font name, and if it is found, replace this range with the desired font, for example, the preferred font and user size, except for bold, etc. its version and continue to replace fonts with each range obtained from the enumeration.

+4
source

The current answer is ok and I have +1. But it was just a clue, not a real solution. If you are looking for a solution to the OP issue, look here .

You should focus on the following:

First, follow these methods:

 - (NSString *)styledHTMLwithHTML:(NSString *)HTML { NSString *style = @"<meta charset=\"UTF-8\"><style> body { font-family: 'HelveticaNeue'; font-size: 20px; } b {font-family: 'MarkerFelt-Wide'; }</style>"; return [NSString stringWithFormat:@"%@%@", style, HTML]; } - (NSAttributedString *)attributedStringWithHTML:(NSString *)HTML { NSDictionary *options = @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }; return [[NSAttributedString alloc] initWithData:[HTML dataUsingEncoding:NSUTF8StringEncoding] options:options documentAttributes:NULL error:NULL]; } 

Later use them like this:

 // This is a string that you might find in your model NSString *html = @"This is <b>bold</b>"; // Apply some inline CSS NSString *styledHtml = [self styledHTMLwithHTML:html]; // Generate an attributed string from the HTML NSAttributedString *attributedText = [self attributedStringWithHTML:styledHtml]; // Set the attributedText property of the UILabel label.attributedText = attributedText; 
+3
source

Source: https://habr.com/ru/post/953364/


All Articles