xamarin ios等宽数字使用UIFontFeature - 有谁知道为什么这是行不通的 [英] xamarin ios monospaced Numbers using UIFontFeature - does anyone know why this doesn't work

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

问题描述

我认为现在IOS应用程序可以强制你的数字在使用自定义字体的时候是等宽的。我找到了一些例子,并使用了一些编译的代码,但是我的编号间隔仍然是成比例的。有没有人得到这个工作,如果是的话,我做错了什么?!

这是我的代码:

  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;

我的自定义字体呈现正常,但是我无法控制数字间距。任何建议非常感谢!

解决方案

首先,您的代码不会使字体等宽。 $ b

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

下面是一个有4个标签的例子,其中1个是Docis Light,2个是Docis Light,第三个是系统字体相同的大小,第四是系统字体与您的调整:


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



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



无论是否调整,他们已经等宽,他们的数字是等宽的。
$ b

最后,如果你需要有等宽数字字体(你的代码所做的),你不需要调整字符的选择。所以这段代码是:

pre $ public $ {
public static class UIFontExtensions
{
public static UIFont MonospacedDigitFont(this 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);


$ / code $ / pre

希望这有助于!我发现有趣的潜水,详细介绍了如何在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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆