如何在Swift 4中为SKLabelNode下划线?
我进行了很多搜索,但似乎没有选择这样做。
import SpriteKit
class GameScene: SKScene {
let mylabel = SKLabelNode(fontNamed:"ChalkboardSE-Regular")
override func didMove(to view: SKView) {
mylabel.text = "Hello World"
mylabel.position = CGPoint(x: UIScreen.main.bounds.width/2 , y: UIScreen.main.bounds.height/2)
addChild(mylabel)
}
}
答案 0 :(得分:-1)
有了从KnightOfDragon收到的链接,我终于可以使它工作了。(感谢KnightOfDragon!),但仅在iOS 11及更高版本中。在iOS 9及更高版本中还有其他使用方式吗?
这是实际代码:
import SpriteKit
class GameScene: SKScene {
let mylabel = SKLabelNode(fontNamed:"ChalkboardSE-Regular")
override func didMove(to view: SKView) {
if #available(iOS 11.0, *) {
mylabel.attributedText = getUnderlinedAttributedString(string: "Hello World")
} else {
mylabel.text = "Hello World"
}
mylabel.position = CGPoint(x: UIScreen.main.bounds.width/2 , y: UIScreen.main.bounds.height/2)
addChild(mylabel)
}
func getUnderlinedAttributedString(string: String) -> NSMutableAttributedString
{
let attributedString = NSMutableAttributedString.init(string: string)
let stringRange = NSMakeRange(0, attributedString.length)
attributedString.beginEditing()
attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: 1, range: stringRange)
attributedString.endEditing()
return attributedString
}
}