2017-04-21 44 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

相關問題