【问题标题】:Setting font size doesn't work after parsing simple HTML into NSAttributedString将简单的 HTML 解析为 NSAttributedString 后设置字体大小不起作用
【发布时间】:2015-04-04 03:37:14
【问题描述】:

我想将简单的 HTML 标记解析为 NSAttributedString,以便可以在 UITextView 上显示格式化文本。我发现 thisthat 帖子应该很容易转换。这是我用过的:

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;
}

到目前为止,这是可行的,但现在我想更改字体大小,因为默认字体非常小。

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;

上面的代码确实可以正确解析它,但字体大小没有改变。我尝试使用NSMutableAttributedString,但它似乎不需要NSData 作为解析参数,就像NSAttributedString 一样。也许合并多个NSAttributedString 是一种选择,但我不知道如何。另一种选择是像这个例子一样投射:

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

但我得到System.InvalidCastException

即使我使用 HTML 解析,如何更改 UITextView 的字体大小?

编辑:

现在我尝试创建我的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;

这确实可以编译,字体更大,并且 HTML 也被解析,但它忽略了 &lt;strong&gt; 例如。文本不是粗体,它应该在哪里。似乎第二个属性覆盖了第一个...

【问题讨论】:

  • 当您尝试将不可变属性字符串转换为可变时,您可能会遇到无效转换异常。很难确定,因为您刚刚说您遇到了异常,但不要确切显示您在哪一行得到它......尝试使用您的 GetAttributedStringFromHtml 响应的 mutableCopy 方法。
  • @IanMacDonald:异常发生在发生转换的行上。现在我可以创建我的NSMutableAttributedString(请参阅已编辑的问题),但我现在还有另一个问题。第二种格式会覆盖第一种格式……也许我应该先增加大小,然后再进行 HTML 解析?
  • 在可变属性字符串上使用enumerateAttribute:inRange:options:usingBlock: 将允许您单独更改每个范围内的字体,而不是整个字符串。
  • 可以使用整个字符串的范围。它将枚举您指定的属性(即字体)的子范围更改。更改您检索的字体的字体大小,然后按子范围重新设置。
  • 问题是(正如我在 Objective-C 中猜测的那样),“strong”参数由粗体字翻译。但是粗体/斜体/fontname/fontsize 在NSFontAttributeName 内部(即:UIStringAttributeKey.Font)。并且属性是一个字典,因此您将替换它的值,因为它的键相同。这就是为什么使用 enumerateAttribute:inRange:options:usingBlock: 会成功的原因。

标签: c# ios xamarin.ios xamarin nsattributedstring


【解决方案1】:

我尝试了一些方法,但都没有奏效。所以我已经在解析 HTML 为什么不使用内联 CSS 语法?

<p style='font-size:17px'><strong>I'm bold.</strong><br/>http://www.google.com</p>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    • 2014-02-14
    • 2023-03-17
    相关资源
    最近更新 更多