如何在 Xamarin Forms 的标签或文本字段中添加富文本或 HTML? [英] How to add Rich Text or HTML in Label or Text field in Xamarin Forms?

查看:46
本文介绍了如何在 Xamarin Forms 的标签或文本字段中添加富文本或 HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Label 或 Xamarin Forms 项目中的任何文本字段中添加富文本格式化文本.我曾尝试为文本使用单独的标签,也尝试使用 WebView 等.但它们都不起作用.使用 Webview 以某种方式工作,但每次更改内容时都需要刷新.使用带有 WebView 的 AJAX 需要一个 Web 服务器,因此它不能在本地/离线工作.

我只想在只读模式下在 Label 上添加富文本,或者可以在运行时从后面的代码中修改.

解决方案

基本上有 2 种方法.

1] 您可以使用 LabelSpan 属性在 Label 中输入格式化文本,就像:

您还可以在Span 中使用Binding 以及SpanLabel 的每个样式和其他属性.


2] 第二种方法是,使用LabelTextType属性并将其设置为TextType=Html".但请注意,Label TextType="Html" 根据应用运行的底层平台,支持的 HTML 标签有限.

我们可以在 C# 代码后面或直接在 XAML 中定义 HTML.

背后的 C# 代码:

Label richTextLabel = new Label{Text = "This is 

在 XAML 中

<小时><块引用>

RE:请参阅此处获取更多文档.

结论:使用 Span 的第一种方法非常可靠,因为所有内容都将按编码呈现.在第二种方法中,在不同的底层平台中,Label 不支持大多数 HTML 标记.因此推荐第一种方法.

I want to add Rich Text or Formatted Text in Label or any Text Field in my Xamarin Forms project. I have tried using separate Labels for the text and also using WebView, etc. But none of them worked. Using a Webview worked somehow, but it needed to refresh every time the contents were changed. Using AJAX with WebView would require a Web Server hence it cannot work locally/offline.

I just want to add Rich text on Label in Read only mode, or can be modified from the code behind at runtime.

解决方案

There are basically 2 ways to do it.

1] You can use Span property of a Label to enter formatted text inside Label Just like:

<Label>
    <Label.FormattedText>
        <FormattedString>
            <Span Text="This is a " FontSize="15" TextColor="Blue"/>
            <Span Text="Rich Text" FontSize="30" TextColor="Red"/>
            <Span Text=" inside a Label in Xamarin Forms" FontSize="15" TextColor="Blue" />
        </FormattedString>
    </Label.FormattedText>
</Label>

You can also use Binding in Span and every style and other properties of Label inside Span.


2] The second method is, use TextType property of Label and set it to TextType="Html". But please note that Label TextType="Html" has limited supported HTML tags according to the underlying platform on which the app runs.

We can define HTML in the C# code behind or directly in XAML.

Inside C# code behind:

Label richTextLabel = new Label
{
    Text = "This is <strong style=\"color:red\">HTML</strong> text.",
    TextType = TextType.Html
};

// OR 
richTextLabel.Text = "This is <strong style=\"color:red\">HTML</strong> text."
richTextLabel.TextType = TextType.Html;
// In XAML set Label Name as <Label x:Name="richTextLabel" />

In XAML

<Label Text="This is &lt;strong style=&quot;color:red&quot;&gt;HTML&lt;/strong&gt; text."
       TextType="Html"  />

<!--The symbols <, >, " needs to be escaped -->


RE: Refer this for more documentation.

Conclusion: The First method using Span is much reliable as everything will be rendered as coded. In the second method, most of the HTML markup is not supported by Label in different underlying platforms. Hence the first method is recommended.

这篇关于如何在 Xamarin Forms 的标签或文本字段中添加富文本或 HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆