我希望在我的iOS应用程序中,为UILabel选择一种等宽字体。
很遗憾,我找不到一种适合的字体。即使是“American Typewriter”也并非等宽字体。请问在XCode中有哪些等宽字体可供使用?
Courier
Courier-Bold
Courier-BoldOblique
Courier-Oblique
CourierNewPS-BoldItalicMT
CourierNewPS-BoldMT
CourierNewPS-ItalicMT
CourierNewPSMT
Menlo-Bold
Menlo-BoldItalic
Menlo-Italic
Menlo-Regular
iosfonts.com维护了一个所有iOS字体的列表。
这个网页会检测您安装的系统字体并返回一个相当全面的列表。
如果您的目的是显示数字而不关心使用的特定字体,您可以这样做:
UIFont.monospacedDigitSystemFontOfSize(
UIFont.systemFontSize(),
weight: UIFontWeightRegular)
Adobe刚刚发布了一款漂亮的等宽字体Source Code Pro,提供TTF和OTF格式。该字体采用开放字体许可证(Open Font License)授权,因此可以在应用程序中免费使用,包括商业应用。
您需要将字体文件作为资源添加到Xcode中,通过将info.plist中的UIAppFonts键设置为指向SourceCodeProRegular.ttf文件。
现在在您的代码中,您可以设置如下:
self.textView.font = [UIFont fontWithName:@"Source Code Pro" size:14];
在 @reggie-pinkham 的回答基础上进行扩展,我创建了JS Bin 来供大家用作实时测试页面。为了节省未来读者的时间,这里提供了几张 Mavericks 上使用 iOS 模拟器的截图:
8.1
8.1
从iOS 13开始,旧金山(SF)等宽字体可用。没有找到在界面构建器中设置的方法,但通过代码可以实现:
let font = UIFont.monospacedSystemFont(ofSize: 17.0, weight: .regular)
SwiftUI
您可以使用字体(Font)的实例方法monospacedDigit
Text("0123456789")
.foregroundColor(.white)
.font(Font.custom("San Francisco", size: 20).monospacedDigit())
// 设置自定义字体 let customFont = UIFont(name: "YourCustomFontName", size: 17.0) UILabel.appearance().font = customFont // 设置等宽数字字体 let monospacedFont = UIFont.monospacedDigitSystemFont(ofSize: 17.0, weight: .regular) UILabel.appearance().font = monospacedFont
请注意,这将应用于整个应用程序中的所有标签。如果您只想在特定标签上使用自定义字体,请使用以下代码:yourLabel.font = customFont
- Rubaiyat Jahan Mumu