2

I want to parse simple HTML markup into a NSAttributedString so that I can display formatted text on an UITextView. I found this and that post where it should be easy to convert. That is what I've used:

public static NSAttributedString GetAttributedStringFromHtml(string html)
{
    NSError error = null;
    NSAttributedString attributedString = new NSAttributedString (NSData.FromString(html), 
        new NSAttributedStringDocumentAttributes{ DocumentType = NSDocumentType.HTML, StringEncoding = NSStringEncoding.UTF8 }, 
        ref error);
    return attributedString;
}

This is working so far, but now I want to change the font size, because the default one is very small.

string content = "<strong>I'm strong.</strong><br/>http://www.google.com";

UITextView textView = new UITextView ();
textView.Editable = false;
textView.Font = UIFont.SystemFontOfSize (25);
textView.Text = content;
textView.AttributedText = GetAttributedStringFromHtml (content);
textView.DataDetectorTypes = UIDataDetectorType.Link;
textView.Selectable = true;

The code above does parse it correctly, but the font size isn't changed. I tried to use NSMutableAttributedString, but it seems that it takes no NSData as argument for parsing like the NSAttributedString does. Perhaps it would be an option to combine multiple NSAttributedString, but I don't know how. Another option would be to cast like this example:

NSMutableAttributedString attributedString = (NSMutableAttributedString) GetAttributedStringFromHtml (content);
attributedString.AddAttribute (UIStringAttributeKey.Font, UIFont.SystemFontOfSize (25), new NSRange (0, content.Length));
textView.AttributedText = attributedString;

but I get System.InvalidCastException.

How can I change the font size of the UITextView even if I use the HTML parsing?

Edit:

Now I tried to create my NSMutableAttributedString:

NSAttributedString parsedString = GetAttributedStringFromHtml (content);
NSMutableAttributedString attributedString = new NSMutableAttributedString (parsedString);
attributedString.AddAttribute (UIStringAttributeKey.Font, UIFont.SystemFontOfSize (17), new NSRange (0, attributedString.Length));
textView.AttributedText = attributedString;

This does compile, the font size is bigger and also the HTML is parsed, but it ignorses the <strong> for example. The text isn't bold, where it should be. It seems that the second attribute overwrites the first one ...

Community
  • 1
  • 1
testing
  • 19,681
  • 50
  • 236
  • 417
  • You're probably getting the invalid cast exception when you try to cast the immutable attributed string as mutable. It's tough to tell for sure since you just said you get an exception, but don't show exactly which line you get it on......... Try using the `mutableCopy` method of your `GetAttributedStringFromHtml` response. – Ian MacDonald Feb 04 '15 at 15:00
  • @IanMacDonald: The exception is occurring on the line where the cast takes place. Now I was able to create my `NSMutableAttributedString` (see edited question), but I've another problem now. The second formatting overwrites the first one ... Perhaps I should first increase the size and than do the HTML parsing? – testing Feb 04 '15 at 15:04
  • 2
    Using `enumerateAttribute:inRange:options:usingBlock:` on your mutable attributed string will allow you to change the font in each range individually instead of on the whole string. – Ian MacDonald Feb 04 '15 at 15:21
  • @IanMacDonald: Currently I want to have the same font size on the whole string. How does `enumerateAttribute:inRange:options:usingBlock:` help me in not overwriting the font style? I think I would have to find out the range first (for the letters which should have the greater font size) and than your mentioned function could perhaps apply the attributes. Perhaps you could post a short example demonstrating this (if I would need this one time). You can also provide your code in Objective-C if you want. – testing Feb 04 '15 at 15:36
  • 1
    You can use the range of the whole string. It will enumerate sub-range changes for the attribute you specify (i.e. font). Change the font size on the font you retrieve and set it back on a per-sub-range basis. – Ian MacDonald Feb 04 '15 at 15:54
  • 1
    The thing, is (as I guess in Objective-C), the "strong" parameter is translated by a bold font. But the bold/italic/fontname/fontsize is INSIDE the `NSFontAttributeName` (i.e.:`UIStringAttributeKey.Font`). And the attributes are a dictionary, so you'll replace it's value since its the same key. That why using enumerateAttribute:inRange:options:usingBlock: will do the trick. – Larme Feb 04 '15 at 16:34

1 Answers1

2

I tried a few things but none of them worked. So I'm already parsing the HTML why not use inline CSS syntax?

<p style='font-size:17px'><strong>I'm bold.</strong><br/>http://www.google.com</p>
testing
  • 19,681
  • 50
  • 236
  • 417