加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – Swift自定义字体Xcode 7

发布时间:2020-12-15 02:03:34 所属栏目:百科 来源:网络整理
导读:我正在使用Swift在Xcode(7.0版Beta)中创建一个游戏,我想在游戏末尾以“gameOver.ttf”的字体显示标签“Game Over”。我已将字体添加到我的资源文件夹。我不知道如何在我的代码中引用它。我可以帮忙吗? 我的代码: let label = SKLabelNode(fontNamed: "gam
我正在使用Swift在Xcode(7.0版Beta)中创建一个游戏,我想在游戏末尾以“gameOver.ttf”的字体显示标签“Game Over”。我已将字体添加到我的资源文件夹。我不知道如何在我的代码中引用它。我可以帮忙吗?
我的代码:

let label = SKLabelNode(fontNamed: "gameOver")
    label.text = "Game Over"
    label.fontColor = SKColor.redColor()
    label.fontSize = 150
    label.position = CGPointMake(0,100)
    label.horizontalAlignmentMode = .Center
    node.addChild(label)

解决方法

这些是向您的应用程序添加自定义字体的步骤:

>在应用程序中添加“gameOver.ttf”字体(Make sure that it’s included in the target)
>修改application-info.plist文件。
>在新行中添加“应用程序提供的字体”键
>并将“gameOver.ttf”添加为数组“应用程序提供的字体”中的新项目。

现在,Interface Builder中将显示该字体。
要在代码中使用自定义字体,我们需要通过名称引用它,但是名称通常与字体的文件名不同

There are 2 ways to find the name:

  1. Install the font on your Mac. Open Font Book,open the font and see
    what name is listed.
  2. Programmatically list the available fonts in your app

对于第二种方法添加这一行是你的应用程序代理的didFinishLaunchingWithOptions

println(UIFont.familyNames())

要列出每个字体系列中包含的字体,在swift 2中:

class func printFonts() {
    for familyName in UIFont.familyNames() {
        print("n-- (familyName) n")
        for fontName in UIFont.fontNamesForFamilyName(familyName) {
            print(fontName)
        }
    }
}

找到您的自定义字体的名称后,您可以使用它:

SKLabelNode(fontNamed: "gameOver") // put here the correct font name

或简单的标签:

cell.textLabel?.font = UIFont(name: "gameOver",size: 16) // put here the correct font name

Useful resource

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读