Swift没有激活标签的风格替代品

时间:2015-09-04 20:43:44

标签: ios swift fonts

我试图为otf字体pluto-sans启用风格替代品,但无论我做什么,默认设置都是它使用的。

当我查看xcode调试中的字体属性时,我得到以下内容

(
        {
        CTFeatureTypeIdentifier = 0;
        CTFeatureTypeName = "All Typographic Features";
        CTFeatureTypeNameID = "-100";
        CTFeatureTypeSelectors =         (
                        {
                CTFeatureSelectorDefault = 1;
                CTFeatureSelectorIdentifier = 0;
                CTFeatureSelectorName = On;
                CTFeatureSelectorNameID = "-101";
            }
        );
    },
        {
        CTFeatureTypeExclusive = 1;
        CTFeatureTypeIdentifier = 6;
        CTFeatureTypeName = "Number Spacing";
        CTFeatureTypeNameID = "-700";
        CTFeatureTypeSelectors =         (
                        {
                CTFeatureSelectorIdentifier = 0;
                CTFeatureSelectorName = "Monospaced Numbers";
                CTFeatureSelectorNameID = "-701";
            },
                        {
                CTFeatureSelectorDefault = 1;
                CTFeatureSelectorIdentifier = 4;
                CTFeatureSelectorName = "No Change";
                CTFeatureSelectorNameID = "-705";
            }
        );
    },
        {
        CTFeatureTypeExclusive = 1;
        CTFeatureTypeIdentifier = 10;
        CTFeatureTypeName = "Vertical Position";
        CTFeatureTypeNameID = "-1100";
        CTFeatureTypeSelectors =         (
                        {
                CTFeatureSelectorDefault = 1;
                CTFeatureSelectorIdentifier = 0;
                CTFeatureSelectorName = "Normal Vertical Position";
                CTFeatureSelectorNameID = "-1101";
            },
                        {
                CTFeatureSelectorIdentifier = 1;
                CTFeatureSelectorName = "Superiors/Superscripts";
                CTFeatureSelectorNameID = "-1102";
            },
                        {
                CTFeatureSelectorIdentifier = 3;
                CTFeatureSelectorName = Ordinals;
                CTFeatureSelectorNameID = "-1104";
            },
                        {
                CTFeatureSelectorIdentifier = 4;
                CTFeatureSelectorName = "Scientific Inferiors";
                CTFeatureSelectorNameID = "-1105";
            }
        );
    },
        {
        CTFeatureTypeIdentifier = 11;
        CTFeatureTypeName = "Contextual Fractional Forms";
        CTFeatureTypeNameID = "-1200";
        CTFeatureTypeSelectors =         (
                        {
                CTFeatureSelectorIdentifier = 2;
                CTFeatureSelectorName = On;
                CTFeatureSelectorNameID = "-1203";
            }
        );
    },
        {
        CTFeatureTypeExclusive = 1;
        CTFeatureTypeIdentifier = 17;
        CTFeatureTypeName = Alternates;
        CTFeatureTypeNameID = "-1800";
        CTFeatureTypeSelectors =         (
                        {
                CTFeatureSelectorDefault = 1;
                CTFeatureSelectorIdentifier = 0;
                CTFeatureSelectorName = "No Alternates";
                CTFeatureSelectorNameID = "-1801";
            },
                        {
                CTFeatureSelectorIdentifier = 1;
                CTFeatureSelectorName = "Alternate 1";
                CTFeatureSelectorNameID = "-1802";
            },
                        {
                CTFeatureSelectorIdentifier = 2;
                CTFeatureSelectorName = "Alternate 2";
                CTFeatureSelectorNameID = "-1803";
            },
                        {
                CTFeatureSelectorIdentifier = 3;
                CTFeatureSelectorName = "Alternate 3";
                CTFeatureSelectorNameID = "-1804";
            },
                        {
                CTFeatureSelectorIdentifier = 4;
                CTFeatureSelectorName = "Alternate 4";
                CTFeatureSelectorNameID = "-1805";
            },
                        {
                CTFeatureSelectorIdentifier = 5;
                CTFeatureSelectorName = "Alternate 5";
                CTFeatureSelectorNameID = "-1806";
            }
        );
    },
        {
        CTFeatureTypeExclusive = 1;
        CTFeatureTypeIdentifier = 21;
        CTFeatureTypeName = "Number Case";
        CTFeatureTypeNameID = "-2200";
        CTFeatureTypeSelectors =         (
                        {
                CTFeatureSelectorIdentifier = 1;
                CTFeatureSelectorName = "Lining Figures";
                CTFeatureSelectorNameID = "-2202";
            },
                        {
                CTFeatureSelectorDefault = 1;
                CTFeatureSelectorIdentifier = 2;
                CTFeatureSelectorName = "No Change";
                CTFeatureSelectorNameID = "-2203";
            }
        );
    },
        {
        CTFeatureTypeExclusive = 1;
        CTFeatureTypeIdentifier = 22;
        CTFeatureTypeName = "Text Spacing";
        CTFeatureTypeNameID = "-2300";
        CTFeatureTypeSelectors =         (
                        {
                CTFeatureSelectorDefault = 1;
                CTFeatureSelectorIdentifier = 7;
                CTFeatureSelectorName = "No Change";
                CTFeatureSelectorNameID = "-2308";
            },
                        {
                CTFeatureSelectorIdentifier = 8;
                CTFeatureSelectorName = "No Kerning";
                CTFeatureSelectorNameID = "-2309";
            }
        );
    },
        {
        CTFeatureTypeIdentifier = 33;
        CTFeatureTypeName = "Case-Sensitive Layout";
        CTFeatureTypeNameID = "-3400";
        CTFeatureTypeSelectors =         (
                        {
                CTFeatureSelectorIdentifier = 0;
                CTFeatureSelectorName = "Capital Forms";
                CTFeatureSelectorNameID = "-3401";
            },
                        {
                CTFeatureSelectorIdentifier = 2;
                CTFeatureSelectorName = "Capital Spacing";
                CTFeatureSelectorNameID = "-3403";
            }
        );
    },
        {
        CTFeatureTypeIdentifier = 35;
        CTFeatureTypeName = "Alternative Stylistic Sets";
        CTFeatureTypeNameID = "-3600";
        CTFeatureTypeSelectors =         (
                        {
                CTFeatureSelectorIdentifier = 2;
                CTFeatureSelectorName = "Stylistic Set 1";
                CTFeatureSelectorNameID = "-3603";
            },
                        {
                CTFeatureSelectorIdentifier = 4;
                CTFeatureSelectorName = "Stylistic Set 2";
                CTFeatureSelectorNameID = "-3605";
            },
                        {
                CTFeatureSelectorIdentifier = 6;
                CTFeatureSelectorName = "Stylistic Set 3";
                CTFeatureSelectorNameID = "-3607";
            }
        );
    },
        {
        CTFeatureTypeIdentifier = 36;
        CTFeatureTypeName = "Contextual Alternates";
        CTFeatureTypeNameID = "-3700";
        CTFeatureTypeSelectors =         (
                        {
                CTFeatureSelectorDefault = 1;
                CTFeatureSelectorIdentifier = 0;
                CTFeatureSelectorName = "Contextual Alternates";
                CTFeatureSelectorNameID = "-3701";
            }
        );
    }
)

上面打印出来的代码如下:

    var a = CTFontCopyFeatures(label.font)
    println(a)

我尝试使用以下代码激活样式选项:

var newfont = UIFontDescriptor(name: "Pluto Sans Cond Medium", size: CGFloat(0.0))

label.font = UIFont(descriptor:        newfont.fontDescriptorByAddingAttributes([UIFontFeatureTypeIdentifierKey: kStylisticAlternativesType, UIFontFeatureSelectorIdentifierKey: kStylisticAltOneOnSelector]), size: CGFloat(17))

我对swift和字体相当新,但据我所知,这应该激活风格的替代品,任何帮助都将非常感激

1 个答案:

答案 0 :(得分:2)

这篇博客文章引导我找到一个解决方案:http://generic.cx/essays/font-descriptors/我最终像这样扩展了uifont:

extension UIFont {
    func activateAlternatives() -> UIFont {
        let oDesc = self.fontDescriptor()
        let features: [NSObject: AnyObject] = [
            UIFontDescriptorFeatureSettingsAttribute: [
                [
                    UIFontFeatureTypeIdentifierKey:
                    kStylisticAlternativesType,
                    UIFontFeatureSelectorIdentifierKey:
                    kStylisticAltOneOnSelector
                ]
                ,[
                    UIFontFeatureTypeIdentifierKey:
                    kStylisticAlternativesType,
                    UIFontFeatureSelectorIdentifierKey:
                    kStylisticAltTwoOnSelector
                ],
                [
                    UIFontFeatureTypeIdentifierKey:
                    kStylisticAlternativesType,
                    UIFontFeatureSelectorIdentifierKey:
                    kStylisticAltThreeOnSelector
                ]
            ]
        ]
        let newDesc = oDesc.fontDescriptorByAddingAttributes(features)
        return UIFont(descriptor: newDesc, size: 0.0)
    }
}

我认为我的问题实际上并不是代码错误,而是我需要使用所有三种替代方法来获取我期待的所有字形