【问题标题】:Converting UIFont to CGFont in swift?快速将 UIFont 转换为 CGFont?
【发布时间】:2014-11-20 00:25:17
【问题描述】:

下面的函数需要传递CGFont

CGFontCreateCopyWithVariations(<#font: CGFont!#>, <#variations: CFDictionary!#>))

【问题讨论】:

  • 好吧,他要的是 Swift,但我想这也回答了这个问题:你需要在这里应用 Objective-C。
  • 非常感谢 Grimxn 和 Peter Branforn

标签: ios objective-c iphone swift uifont


【解决方案1】:

这是一个小例子:

var font = UIFont.systemFontOfSize(15.0)
var fontName = font.fontName as NSString
var cgFont = CGFontCreateWithFontName(fontName);

var copiedFont = CGFontCreateCopyWithVariations(cgFont, nil)

【讨论】:

  • 知道了。非常感谢:)
【解决方案2】:

在 Swift 4 中遇到了这个问题,接受的答案不再有效。以下取了UIFont 并给了我一个可用的CGFont

if let uiFont = UIFont(name: "Helvetica-Bold", size: 36.0),
    let cgFont = CGFont(uiFont.fontName as CFString) {
        // Do stuff        
    }

【讨论】:

  • 我们如何将 UIFont 的 font-size 添加到 CGFont 中?
  • 当您使用 CGContext 绘制字体时,您必须在 CGContext 上调用 setFontSize(_ size: CGFloat)
猜你喜欢
  • 2011-08-13
  • 1970-01-01
  • 2011-10-06
  • 2012-03-01
  • 1970-01-01
  • 2010-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多