2017-04-21 16 views
1

我想通过启用它支持的OpenType功能来修改旧金山字体,例如one storey aopened four and six。此设置应仅适用于正文文体样式在iOS系统字体中启用正文文本样式的排版功能

文本样式是设置在Storyboard属性检查器和不在代码

下面的代码,由于某种原因,不能正常工作:

let bodyFontDescriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .body) 
// This isn’t working either: let bodyFontDescriptor = UIFont.preferredFont(forTextStyle: .body).fontDescriptor 
let oneStoreyAFontDescriptor = bodyFontDescriptor.addingAttributes(
    [ 
     UIFontDescriptorFeatureSettingsAttribute: 
     [ 
      [ 
       UIFontFeatureTypeIdentifierKey: kStylisticAlternativesType, 
       UIFontFeatureSelectorIdentifierKey: kStylisticAltSevenOnSelector 
      ] 
     ] 
    ] 
) 
textView.font = UIFont(descriptor: oneStoreyAFontDescriptor, size: 0.0) 

我在做什么错?一般来说,这是正确的方法吗?

+0

无法理解**一层a或打开四,六**。 –

+1

@dahiya_boy [打开四和六]的示例(https://i.stack.imgur.com/ogG7Z.gif)。 [一层]的示例(https://en.wikipedia.org/wiki/A#/media/File:LowercaseA.svg)。 – Boletrone

回答

1

看起来你需要玩围绕选择器。结合SixSeven替代下面的代码后已开始工作:

let six = [UIFontFeatureTypeIdentifierKey: kStylisticAlternativesType, 
         UIFontFeatureSelectorIdentifierKey: kStylisticAltSixOnSelector] 
let seven = [UIFontFeatureTypeIdentifierKey: kStylisticAlternativesType, 
         UIFontFeatureSelectorIdentifierKey: kStylisticAltSevenOnSelector] 
let descriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .body).addingAttributes([UIFontDescriptorFeatureSettingsAttribute: [six, seven]]) 
let font = UIFont(descriptor: descriptor, size: 0.0) 
label.font = font 

enter image description here

0

我找到了原因,我无法应用字体属性将文本样式。我检查了Dynamic Type Automatically Adjusts Font的检查标记属性检查器的一个视图。这会干扰激活在代码中设置的字体特征。

dynamic type automatically adjusts font

相关问题