Mix bold text to plain text in labels

假装没事ソ 提交于 2021-02-05 10:40:39

问题


I save my text data in a sqlite database, I am able to store them, read them and etc.

To load it in The app I do something like this in page.xaml.cs:

new Label() {Text = myDbStringData};

However doing like that I can only save plain text, so here's my question, how can i store and read mixed text Bold and plain text in DB and then be able to show it in a Label?

I want to save something like this:

Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit...

Thanks for your help.


回答1:


I guess you cannot store a formatted text in your data base. There is no way for it to what I know. However, you can format your text for the label using the FormattedText property of the Label and setting FormattedString.

Refer Xamarin documentation here for more details.

Refer below example code.

<Label LineBreakMode="WordWrap">
    <Label.FormattedText>
        <FormattedString>
            <Span Text="Hello Name, " TextColor="Red" FontAttributes="Bold" />
            <Span Text="Welcome to , " Style="{DynamicResource BodyStyle}" />
            <Span Text="Xamarin Forms." FontAttributes="Italic" FontSize="Small" />
        </FormattedString>
    </Label.FormattedText>
</Label>

As of now the only way to store formatted text in your database would be to format it using HTML and save it. You can use the Display HTML feature of Label to render that in UI. Documentation details here.

Refer below code more reference.

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

<Label TextType="Html">
    <![CDATA[
    This is <strong style="color:red">HTML</strong> text.
    ]]>
</Label>



回答2:


If you trying to store and Read a single string into your SQLite DB then use TextType="Html" property in Label control.

like...

<Label Text="{Binding YourViewModelStringProperty}" TextType="Html">



回答3:


Hello @Riccardo Raffini,

You can store Text in DB like this, "Neque <strong>porro</strong>"

then in Xaml Add Text Type to Html,

<Label Text="YourTextHere" TextType="Html">

Hope this helps.



来源:https://stackoverflow.com/questions/60051078/mix-bold-text-to-plain-text-in-labels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!