【问题标题】:How to set Xamarin.Forms Label Font Bold如何设置 Xamarin.Forms 标签字体粗体
【发布时间】:2014-08-01 17:15:30
【问题描述】:

Xamarin.Forms 中,我正在使用 Label 并尝试设置 Font

以下代码有效:-

Label label1 = new Label();
label1.Font = Font.SystemFontOfSize(10);

但是尝试指定 字体属性 例如:-

Label label1 = new Label();
label1.Font = Font.SystemFontOfSize(10, FontAttributes.Bold);

正在阻止 ContentPage 呈现异常。

有一个 Font.BoldSystemFontOfSize() 可以使用,但是这意味着要弃用,所以我现在尝试使用 Font.SystemOfSize 代替.

如何使用它?

【问题讨论】:

  • Font.SystemFontOfSize() 已弃用。改用FontAttributes,即FontAttributes = FontAttributes.Bold

标签: xamarin.forms


【解决方案1】:

我想我迟到了。 但我仍然想提一下,现在可以使用 XAML 来完成。 以下 XAML 将提供所需的输出。

<Label Text="Hello Label" FontSize="20" FontAttributes="Bold"/>

您可以参考以下链接,了解有关在 Xamarin.Forms 中使用字体的更多信息。

Working With Fonts in XForms

【讨论】:

  • 我更喜欢 FontAttributes 因为字体已经过时,可能以后会删除
【解决方案2】:

按照文档Xamarin.Forms - Working with Fonts,您应该使用以下代码:

Label label1 = Font.SystemFontOfSize (10, FontAttributes.Bold | FontAttributes.Italic)

同一页面声明 BoldSystemFontOfSize 已弃用。

【讨论】:

  • 这与我使用的代码相同。也许只有在 ContentView 中使用 Label 控件才是问题。
  • 抱歉,这正是您使用的(脸红)。为什么要声明标签而不是使用之前声明的标签?请显示整个表单代码
  • 错字 - 我会更正,因为我试图为我正在使用的变量提供一些上下文。
【解决方案3】:

这是在我的项目中有效的一段代码:

new Label {
   Text = "text goes here",
   Font = Font.SystemFontOfSize (NamedSize.Medium)
              .WithAttributes (FontAttributes.Bold),
}

这允许您不指定某个字体大小,而是使用标签默认使用的字体大小

【讨论】:

  • 这很好用。只是好奇,您是否设法使用 .WithAttributes 同时使用 BoldItalic
  • 我没有,你可以试试FontAttributes.Bold | FontAttributes.Italic
  • 下划线怎么办?好像没有办法了
  • @NeelamPrajapati 尝试 XLabs 包中的 ExtendedLabel github.com/XLabs/Xamarin-Forms-Labs/wiki/ExtendedLabel
【解决方案4】:

我遇到过类似的情况,DataTemplate 内的标签的FontAttributes="Bold" ListView 在 iOS 中没有呈现为粗体。 Android 渲染良好。

当不在DataTemplate 内时,粗体Label 会正确呈现。

原因是默认字体在 iOS 上没有可用的粗体。当我将Font="Arial" 添加到Label 时,它在iOS 中正确呈现了粗体。

我分享这个以防其他人遇到同样的问题。

【讨论】:

    【解决方案5】:

    如果您需要多个需要加粗字体的地方,那么您最好创建一种样式。在 app.cs 中全局使用或不使用键或在页面级别。您可以将所需的所有属性设置为适当的值并在任何您想要的地方使用。

    参考请查看page

    【讨论】:

      猜你喜欢
      • 2020-09-24
      • 2016-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-24
      • 1970-01-01
      相关资源
      最近更新 更多