我不知道如何使用swift 4.2中的NSAttributedString为大字体大小的文本加下划线
例如,此代码未加下划线,但如果我将字体大小更改为12或更低,则可以正常工作并且文本带有下划线。
如何在字体大小为25或更高的情况下为文本加下划线?
import SpriteKit
class GameScene: SKScene {
let mylabel = SKLabelNode()
override func didMove(to view: SKView) {
let myAttributes : [NSAttributedString.Key: Any] = [
NSAttributedString.Key.font : UIFont(name: "ChalkboardSE-Regular", size: 25.0)!,
NSAttributedString.Key.underlineStyle : NSUnderlineStyle.single.rawValue]
mylabel.attributedText = NSMutableAttributedString(string: "Hello World", attributes: myAttributes)
mylabel.position = CGPoint(x: UIScreen.main.bounds.width/2 , y: UIScreen.main.bounds.height/2)
addChild(mylabel)
}
}
class UnderlinedLabel: UILabel {
override var text: String? {
didSet {
guard let text = text else { return }
let textRange = NSMakeRange(0, text.count)
let attributedText = NSMutableAttributedString(string: text)
attributedText.addAttribute(NSAttributedString.Key.underlineStyle , value: NSUnderlineStyle.single.rawValue, range: textRange)
// Add other attributes if needed
self.attributedText = attributedText
}
}
}
之后直接将UnderlineLabel类分配给特定的Label到StoryBoard。