【问题标题】:Is there a way that I can have a word inside a label appear in a bold font?有没有办法让标签内的单词以粗体显示?
【发布时间】:2017-10-27 06:14:26
【问题描述】:

我有这个 Xaml:

<StackLayout Spacing="0" Margin="20">
   <Label Text="I would like the word" />
   <Label Text="HERE"  />
   <Label Text="to be in a bold font and red color"  />
</StackLayout>

这给出了这个:

I would like the word
HERE
to be in a bold font

有什么方法可以组合这些标签,使 HERE 一词以粗体和红色显示,并且所有词都出现在一行上?我要找的是这个:

I would like the word HERE to be in a bold font

请注意,我使用的是 Xamarin.Forms,但我也标记了 Xamarin.iOS,因为我想知道这是否可以使用渲染器并想知道是否可以在 iOS 中完成?

【问题讨论】:

    标签: xamarin xamarin.ios xamarin.forms


    【解决方案1】:

    使用FormattedText

    <Label.FormattedText>
      <FormattedString>
         <Span Text="I would like the word " />
         <Span Text="HERE" ForegroundColor="Red" FontAttributes="Bold" />
         <Span Text="to be in a bold font" />
       </FormattedString>
    </Label.FormattedText>
    

    【讨论】:

    • 谢谢,您的回答非常好。当我意识到我希望将其显示在合理的标签中时,我还提出了另一个问题。你认为这可能吗? stackoverflow.com/questions/46970713/…
    • 如何使用绑定视图模型设置文本值?
    • @ZiyadGodil :我想我要么扩展 Span 以支持可绑定文本,要么为 FormattedText 使用转换器。不知道为什么 XF 团队将 Span 上的 Text 属性保留为简单属性,而不是使其可绑定。
    【解决方案2】:

    如果您使用的是 Xamarin 表单,则可以使用 FontAttributes 样式属性。

    <StackLayout Spacing="0" Margin="20">
       <Label Text="I would like the word" />
       <Label Text="HERE"  />
       <Label Text="to be in a bold font and red color"
              FontAttributes="Bold" />
    </StackLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-16
      • 2022-01-12
      • 2018-06-05
      • 1970-01-01
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      相关资源
      最近更新 更多