Swift自定义字体Xcode 7 [英] Swift Custom Fonts Xcode 7
问题描述
我的代码:
let label = SKLabelNode(fontNamed:gameOver)
label.text = 游戏结束
label.fontColor = SKColor.redColor()
label.fontSize = 150
label.position = CGPointMake(0,100)
label.horizontalAlignmentMode = .Center
node.addChild(label)
是为您的应用程序添加自定义字体的步骤:
$ b
- 在您的应用程序中添加gameOver.ttf字体(确保它包含在目标中)
- 修改application-info.plist文件。
- 在新行中添加应用程序提供的字体 $ b
- ,并将gameOver.ttf添加为Array 应用程序提供的字体中的新项目。
现在字体将在Interface Builder中可用。
要在代码中使用自定义字体,我们需要引用它的名称,但名称通常与字体的文件名不一样。
lockquote
有两种方法可以找到名称:
- 在Mac上安装字体。打开字体书,打开字体并查看
列出的名字。 - 以编程方式列出应用程序中可用的字体
为第二种方法添加此行是您的应用程序委托的 didFinishLaunchingWithOptions
println(UIFont.familyNames())
$ b $
$ p $
class func printFonts(){
为UIFont.familyNames()中的familyName {$ b $ print(\\\
-- \(familyName)\\\
)
为UIFont.fontNamesForFamilyName(familyName)中的fontName {
print(fontName)
}
}
}
after找到你自定义字体的名字,你可以像这样使用它:
SKLabelNode(fontNamed:gameOver)//放在这里正确的字体名称
或在一个简单的标签:
cell.textLabel?.font = UIFont(name:gameOver,size:16)字体名称
I am creating a game in Xcode (Version 7.0 Beta) using Swift, and I would like to display the label "Game Over" at the end of the game in the font "gameOver.ttf". I have added the font to my resources folder. I do not know how to reference it in my code. Can I please get help? My Code:
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)
These are the steps to add a custom font to you application:
- Add "gameOver.ttf" font in your application ( Make sure that it's included in the target)
- Modify the application-info.plist file.
- Add the key "Fonts provided by application" in a new row
- and add "gameOver.ttf" as new item in the Array "Fonts provided by application".
Now the font will be available in Interface Builder. To use the custom font in code we need to refer to it by name, but the name often isn’t the same as the font’s filename
There are 2 ways to find the name:
- Install the font on your Mac. Open Font Book, open the font and see what name is listed.
- Programmatically list the available fonts in your app
for the second approach add this line is your app delegate’s didFinishLaunchingWithOptions
println(UIFont.familyNames())
To list the fonts included in each font family, in swift 2:
class func printFonts() {
for familyName in UIFont.familyNames() {
print("\n-- \(familyName) \n")
for fontName in UIFont.fontNamesForFamilyName(familyName) {
print(fontName)
}
}
}
after finding the name of your custom font you may use it like this:
SKLabelNode(fontNamed: "gameOver") // put here the correct font name
or in a simple label :
cell.textLabel?.font = UIFont(name: "gameOver", size: 16) // put here the correct font name
这篇关于Swift自定义字体Xcode 7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!