ios - 如何在 Xcode Playground 中实现自定义字体

标签 ios swift xcode fonts swift-playground

我想用自定义字体更改标签的字体,但编译器给我一个问题: 在展开 Optinal 值时意外发现 nil。 我认为这个问题是由于 Xcode 无法识别我的字体文件 Brandon_reg.otf。我做错什么了?下载 Playground :https://ufile.io/940cc

import UIKit 
import PlaygroundSupport

var view = UIView(frame: UIScreen.main.bounds)

let fontURL = Bundle.main.url(forResource: "Brandon_reg", withExtension: "otf")
CTFontManagerRegisterFontsForURL(fontURL! as CFURL, CTFontManagerScope.process, nil)
var font = UIFont(name: "horrendo", size: 30)
var attrs = [NSFontAttributeName : font!,
             NSForegroundColorAttributeName : UIColor.white,
             NSBaselineOffsetAttributeName : 0.0] as [String : Any]
let nameAttrSring = NSAttributedString(string: "Brandon_reg", attributes: attrs)

let mainLabel: UILabel = {
    let label = UILabel()
    label.font = font
    label.textColor = .white
    label.translatesAutoresizingMaskIntoConstraints = false
    label.textAlignment = .center
    label.numberOfLines = 0
    return label
}()

view.addSubview(mainLabel)
PlaygroundPage.current.liveView = view
PlaygroundPage.current.needsIndefiniteExecution = true

最佳答案

您的字体文件中没有名为“horrendo”的字体。这对我有用:

 var font = UIFont(name: "Brandon Grotesque", size: 30)

关于ios - 如何在 Xcode Playground 中实现自定义字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43018332/

相关文章:

ios - 滚动离开屏幕(即不可见)时如何访问 CollectionView header

ios - 设置 FetchedResultsController 中的特殊结果

arrays - 如何在 Swift 中扩展类型化数组?

ios - UIStackView 不可见

xcode - 将 Xcode 包版本设置为 git hash

objective-c - 为 Objective-C/XCode 组织#import 语句

php - 如何将变量从 iOS 发送到 php 文件

iphone - 多个类 iOS 中的一个字符串

ios - 减少文本字段中左 View 和光标之间的间隙

iphone - Cocos2d removeChild 不工作