CATextLayer.font
プロパティは不透明ですCFTypeRef
です。その参照をUIFont
に変換する方法はありますか?私はそれがオプションのUIFontを返すかどうか気にしない。 CTFont
、CGFont
、文字列名またはUIFont
:CATextLayerフォントをUIFontに変換します。
extension CATextLayer {
public var typeFace:UIFont? {
let name:String
switch font {
case let ctFont as CTFont :
// If you assign a UIFont to CATextLayer.font
// it is implicitly converted to CTFont,
// so this case will be called.
guard let _name = CTFontCopyName(ctFont, kCTFontPostScriptNameKey) else {
return nil
}
name = _name as NSString as String
case let cgFont as CGFont :
guard let _name = cgFont.postScriptName else {
return nil
}
name = _name as NSString as String
case let string as NSString :
name = string as String
case let string as String :
name = string
default:
return nil
}
return UIFont(name:name, size:fontSize)
}
}
あなたはこのように行うことができます
これは、CTFont、CGFont、NSFont(macOSのみ)のインスタンス、またはdocにしたがってフォントを指定する文字列である可能性があります。だからhttps://stackoverflow.com/questions/6714858/iphone-convert-ctfont-to-uifont、https://stackoverflow.com/questions/9205709/how-to-convert-cgfontref-to-uifontなど?タイプに応じて、機能同等の名前はSwiftで利用できるようにする必要があります。タイプを取得するにはhttps://stackoverflow.com/questions/3471964/determining-what-a-cftyperef-is? – Larme
テキストレイヤーを作成して設定したので、どのフォントを割り当てたかわからないのですか? – matt
簡単。これは一般的なフレームワークの一部であり、私はいくつかの場所で異なるフォントでテキストレイヤーを使用します。実際、以下の私の答えは、 'get'と' set'を必要とするプロトコル実装の修正版です。前提はありません。 –