本文介绍了使用 UIFontFeature 的 xamarin ios 等宽数字 - 有谁知道为什么这不起作用'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为现在可以在 IOS 应用程序中使用自定义字体强制您的数字等宽.我找到了示例并使用了一些编译代码,但我的数字间距仍然成比例.有没有人让这个工作,如果是这样,我做错了什么?!

这是我的代码:

UIFont bigNumberFont = UIFont.FromName("Dosis-Light", 60f);var originalDescriptor = bigNumberFont.FontDescriptor;var attributes = new UIFontAttributes(new UIFontFeature(CTFontFeatureNumberSpacing.Selector.MonospacedNumbers),新 UIFontFeature((CTFontFeatureCharacterAlternatives.Selector)0));var newDesc = originalDescriptor.CreateWithAttributes(attributes);UIFont bigNumberMono = UIFont.FromDescriptor(newDesc, 60f);lbCurrentPaceMinute.Font = bigNumberMono;

我的自定义字体呈现良好,但我目前无法控制数字间距.非常感谢任何建议!

解决方案

首先,您的代码没有使字体等宽.

您正在调整字体以在等宽模式下呈现数字.所以你所有的数字都将具有相同的宽度.

下面是一个带有 4 个标签的示例,1 个是 Docis Light,2nd 是经过调整的 Docis Light,第 3 是相同大小的系统字体,第 4 是经过调整的系统字体:

如您所见,Docis Light 已经支持开箱即用的等宽数字功能,无需任何调整.

如果您需要使用等宽字体,则必须使用自定义等宽字体(设计为等宽)或者您可以使用内置的 iOS 等宽字体,例如 Courier 或 Menlo(在

无论是否经过调整,它们都已经是等宽的,并且它们的数字也是等宽的.

最后,如果你需要等宽数字字体(你的代码做什么),你不需要调整字符替代.所以代码将是:

公共静态类 UIFontExtensions{public static UIFont MonospacedDigitFont(这个UIFont字体){var originalDescriptor = font.FontDescriptor;var monospacedNumbersFeature = new UIFontFeature(CTFontFeatureNumberSpacing.Selector.MonospacedNumbers);var attributes = new UIFontAttributes(monospacedNumbersFeature);var newDescriptor = originalDescriptor.CreateWithAttributes(attributes);返回 UIFont.FromDescriptor(newDescriptor, font.PointSize);}}

希望这有帮助!我发现深入了解如何在 iOS 中调整字体和不可以调整字体很有趣.

I thought it was now possible in IOS apps to force your numbers to be monospaced when using a custom font. I've found examples and used some code that compiles but my number spacing is still proportional. Has anyone got this working and if so what am I doing wrong?!

Here is my code:

UIFont bigNumberFont = UIFont.FromName("Dosis-Light", 60f);

var originalDescriptor = bigNumberFont.FontDescriptor;
var attributes = new UIFontAttributes(new UIFontFeature(CTFontFeatureNumberSpacing.Selector.MonospacedNumbers),
            new UIFontFeature((CTFontFeatureCharacterAlternatives.Selector)0));

var newDesc = originalDescriptor.CreateWithAttributes(attributes);

UIFont bigNumberMono = UIFont.FromDescriptor(newDesc, 60f);

lbCurrentPaceMinute.Font = bigNumberMono;

My custom font renders fine but I cant get any control over number spacing as of yet. Any suggestions greatly appreciated!

解决方案

First, your code is not making font monospaced.

You are tweaking font to render digits in monospace mode. So all your digits will have same width.

Below is an example with 4 labels, 1 is Docis Light, 2nd is Docis Light with your tweak, 3rd is system font of same size, 4th is system font with your tweak:

As you see, Docis Light is already supporting monospace digits feature out of the box with no tweak.

If you need to use monospaced font, you have to use custom monospaced font (designed to be monospaced) or you can use built-in iOS monospaced fonts such as Courier or Menlo (See all available iOS fonts at http://iosfonts.com/)

This is how they look like with same scenario:

With or without tweaking, they are already monospaced and their digits are monospaced as well.

Finally, if you need to have monospaced digits font (what your code does) you don't need to tweak character alternative. So the code would be:

public static class UIFontExtensions
{
    public static UIFont MonospacedDigitFont(this UIFont font)
    {
        var originalDescriptor = font.FontDescriptor;
        var monospacedNumbersFeature = new UIFontFeature(CTFontFeatureNumberSpacing.Selector.MonospacedNumbers);
        var attributes = new UIFontAttributes(monospacedNumbersFeature);
        var newDescriptor = originalDescriptor.CreateWithAttributes(attributes);
        return UIFont.FromDescriptor(newDescriptor, font.PointSize);
    }
}

Hope this helps! I found it interesting diving into details how you can and cannot tweak fonts in iOS.

这篇关于使用 UIFontFeature 的 xamarin ios 等宽数字 - 有谁知道为什么这不起作用'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 10:41