SKLabelNode算是SpriteKit里的UILabel的替代品了,但是用起来好像不如UILabel好用。
我在开发的时候遇到这样个问题,SKLabelNode在修改lineBreakMode后没有效果。
源码如下
let textString = "草长莺飞二月天拂堤杨柳醉春烟"
//UILabel
uiLabel = UILabel(frame: CGRect(x: 100, y: size.height / 3 - 45, width: size.width - 200, height: 90))
uiLabel.backgroundColor = .white
uiLabel.font = UIFont(name: "PingFangSC-Regular", size: 17)
uiLabel.textAlignment = .center
uiLabel.lineBreakMode = .byTruncatingMiddle
uiLabel.numberOfLines = 1
uiLabel.textColor = .black
uiLabel.text = textString
view.addSubview(uiLabel)
//SKLabel
let labelbackground = SKSpriteNode(color: .white, size: CGSize(width: size.width - 200, height: 90))
labelbackground.anchorPoint = CGPoint(x: 0.5, y: 0.5)
labelbackground.position = CGPoint(x: 0, y: 0 - size.height / 6)
addChild(labelbackground)
skLabel = SKLabelNode(fontNamed: "PingFangSC-Regular")
skLabel.color = .white
skLabel.position = CGPoint(x: 0, y: 0)
skLabel.horizontalAlignmentMode = .center
skLabel.verticalAlignmentMode = .center
skLabel.lineBreakMode = .byTruncatingMiddle
skLabel.preferredMaxLayoutWidth = size.width - 200
skLabel.numberOfLines = 1
skLabel.fontSize = 17
skLabel.fontColor = .black
skLabel.text = textString
labelbackground.addChild(skLabel)
目前还没找到问题或者解决方案。