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

如何在Flutter中关闭字体连字?使用Fira Code字体寻求帮助

关闭Fira Code字体的连字方法

Fira Code的连字基于OpenType的liga(标准连字)和dlig(可选连字)特性实现,不同平台的TextStyle可以通过禁用这些特性来关闭连字,以下是常见场景的实现方式:

Flutter

在Flutter的TextStyle中,通过fontFeatures参数指定禁用连字规则:

Text(
  "示例文本",
  style: TextStyle(
    fontFamily: 'FiraCode',
    fontFeatures: [
      FontFeature.disable('liga'), // 禁用标准连字
      FontFeature.disable('dlig'), // 禁用可选连字
    ],
  ),
)

Android

XML 布局方式

直接在TextView的属性中设置字体特性:

<TextView
    android:text="示例文本"
    android:fontFamily="FiraCode"
    android:fontFeatureSettings="'liga' 0, 'dlig' 0"/>

代码方式(Kotlin)

val textView = findViewById<TextView>(R.id.targetTextView)
textView.typeface = Typeface.create("FiraCode", Typeface.NORMAL)
textView.fontFeatureSettings = "'liga' 0, 'dlig' 0"

iOS

通过NSAttributedString为文本设置禁用连字的属性:

let targetLabel = UILabel()
let contentText = "示例文本"
guard let firaCodeFont = UIFont(name: "FiraCode", size: 16) else { return }

let textAttributes: [NSAttributedString.Key: Any] = [
    .font: firaCodeFont,
    .fontFeatureSettings: [
        [
            UIFontFeatureTypeIdentifierKey: kLigaturesType,
            UIFontFeatureSelectorIdentifierKey: kLigaturesOffSelector
        ]
    ]
]

targetLabel.attributedText = NSAttributedString(string: contentText, attributes: textAttributes)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 22:57:19