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 ...

6
  • 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. Commented Feb 4, 2015 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? Commented Feb 4, 2015 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. Commented Feb 4, 2015 at 15:21
  • 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. Commented Feb 4, 2015 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. Commented Feb 4, 2015 at 16:34

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.