Exemplo n.º 1
0
        private void SetText(string html)
        {
            // Create HTML data sting
            var stringType = new NSAttributedStringDocumentAttributes
            {
                DocumentType = NSDocumentType.HTML
            };
            var nsError = new NSError();

            var          htmlData = NSData.FromString(html, NSStringEncoding.Unicode);
            NSDictionary dict     = new NSDictionary();

            using (var htmlString = new NSAttributedString(htmlData, stringType, out dict, ref nsError))
            {
                NSMutableAttributedString mutableHtmlString = htmlString.RemoveTrailingNewLine();

                mutableHtmlString.EnumerateAttributes(new NSRange(0, mutableHtmlString.Length), NSAttributedStringEnumeration.None,
                                                      (NSDictionary value, NSRange range, ref bool stop) =>
                {
                    NSMutableDictionary md = new NSMutableDictionary(value);
                    var font = md[UIStringAttributeKey.Font] as UIFont;
                    if (font != null)
                    {
                        md[UIStringAttributeKey.Font] = Control.Font.WithTraitsOfFont(font);
                    }
                    else
                    {
                        md[UIStringAttributeKey.Font] = Control.Font;
                    }

                    var foregroundColor = md[UIStringAttributeKey.ForegroundColor] as UIColor;
                    if (foregroundColor == null || foregroundColor.IsEqualToColor(UIColor.Black))
                    {
                        md[UIStringAttributeKey.ForegroundColor] = Control.TextColor;
                    }
                    mutableHtmlString.SetAttributes(md, range);
                });

                mutableHtmlString.SetLineHeight(Element);
                mutableHtmlString.SetLinksStyles(Element);
                Control.AttributedText = mutableHtmlString;
            }
        }