You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改UITextAttributes以设置新的字体大小与字重?

Fixing UITextAttributes Font Setup Errors in Xamarin

Hey there! Let's break down and fix those two CS1503 errors you're seeing with your UITextAttributes code right away.

Error 1: Cannot convert 'double' to 'System.nfloat'

The UIFont.SystemFontOfSize() method expects the first parameter (font size) to be an nfloat type, but you're passing a double (20.0). To fix this, you have two simple options:

  • Append an f to the number to make it a float (which is compatible with nfloat): 20.0f
  • Explicitly cast it to nfloat: (nfloat)20.0

Error 2: Cannot convert 'Xamarin.Forms.FontAttributes' to 'UIKit.UIFontWeight'

You're using FontAttributes.Bold from Xamarin.Forms, but the method requires a UIFontWeight enum value from UIKit. Just swap out the Xamarin.Forms enum for the UIKit-specific one: UIFontWeight.Bold.

Corrected Code

Here's the fully fixed version of your code:

UITextAttributes att = new UITextAttributes();
att.Font = UIFont.SystemFontOfSize(20.0f, UIFontWeight.Bold); // size and weight

To quickly recap the changes:

  • We adjusted the size to a float (20.0f) to match the method's nfloat parameter requirement
  • We replaced the Xamarin.Forms font attribute with the UIKit font weight enum that the method expects

This should resolve both compilation errors and get your text styling working as intended.

内容的提问来源于stack exchange,提问作者Alan2

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:08:18