Beispiel #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);

            using var htmlString = new NSAttributedString(htmlData, stringType, out _, ref nsError);
            var mutableHtmlString = htmlString.RemoveTrailingNewLines();

            mutableHtmlString.EnumerateAttributes(new NSRange(0, mutableHtmlString.Length), NSAttributedStringEnumeration.None,
                                                  (NSDictionary value, NSRange range, ref bool stop) =>
            {
                var 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;
        }