我正在尝试创建具有大纲的文本。我目前正在使用SKLabelNode和NSAttributedString,现在您可以在SpriteKit中使用iOS 11。问题是,如果笔画宽度太厚,那么轮廓就会被看起来是SKLabelNode的边框所切断。请看下面的图像和代码。

extension SKLabelNode {
func addStroke(_ strokeColor:UIColor) {
let font = UIFont(name: self.fontName!, size: self.fontSize)
let attributes:[NSAttributedStringKey:Any] = [.strokeColor: strokeColor, .strokeWidth: 20.0, .font: font!]
let attributedString = NSMutableAttributedString(string: " \(self.text!) ", attributes: attributes)
let label1 = SKLabelNode()
label1.horizontalAlignmentMode = self.horizontalAlignmentMode
label1.text = self.text
label1.zPosition = -1
label1.attributedText = attributedString
self.addChild(label1)
}
}我查看了扩展用作边框文本的SKLabelNode的框架,但这是一个只能获取的属性。我试图添加前导/尾随空格,但它们似乎是自动修剪的。对strokeWidth使用负值,但会创建内部笔画,我更愿意使用外部笔画。
有什么想法吗?提前感谢您的帮助!麦克
发布于 2018-04-25 09:45:30
.foregroundColor来填充。以下是代码:
extension SKLabelNode {
func addStroke(color:UIColor, width: CGFloat) {
guard let labelText = self.text else { return }
let font = UIFont(name: self.fontName!, size: self.fontSize)
let attributedString:NSMutableAttributedString
if let labelAttributedText = self.attributedText {
attributedString = NSMutableAttributedString(attributedString: labelAttributedText)
} else {
attributedString = NSMutableAttributedString(string: labelText)
}
let attributes:[NSAttributedStringKey:Any] = [.strokeColor: color, .strokeWidth: -width, .font: font!, .foregroundColor: self.fontColor!]
attributedString.addAttributes(attributes, range: NSMakeRange(0, attributedString.length))
self.attributedText = attributedString
}
}https://stackoverflow.com/questions/48407034
复制相似问题