What would be the best approach for outlining or dropshadowing a font?

后端 未结 10 1806
不思量自难忘°
不思量自难忘° 2020-12-14 11:00

I\'m not finding any support for dropshadow or outline of a font in Sprite Kit. For the dropshadow, I\'m guessing I could create a second SKLabelNode and offset it behind t

10条回答
  •  隐瞒了意图╮
    2020-12-14 11:38

    This worked for me:

    http://battleofbrothers.com/sirryan/outline-text-in-spritekit

    Here's the code I used in conjunction with ASAttributedLabelNode (some of it's specific to me like the font name/size/fillcolor/outlinecolor but of course just use your own):

    func outlinedCenteredString(string : String, size: CGFloat) -> NSAttributedString
    {
        var myMutableString : NSMutableAttributedString
        var font =  UIFont(name: "Super Mario 256", size: size)!
        var alignment : CTTextAlignment = CTTextAlignment.TextAlignmentCenter
        let alignmentSetting = [CTParagraphStyleSetting(spec: .Alignment, valueSize: Int(sizeofValue(alignment)), value: &alignment)]
        var paragraphRef = CTParagraphStyleCreate(alignmentSetting, 1)
    
        let textFontAttributes = [
            NSFontAttributeName : font,
            // Note: SKColor.whiteColor().CGColor breaks this
            NSForegroundColorAttributeName: UIColor.yellowColor(),
            NSStrokeColorAttributeName: UIColor.blackColor(),
            // Note: Use negative value here if you want foreground color to show
            NSStrokeWidthAttributeName:-3
            //,NSParagraphStyleAttributeName: paragraphRef
        ]
    
        myMutableString = NSMutableAttributedString(string: string, attributes: textFontAttributes as [NSObject : AnyObject])
    
        let para = NSMutableParagraphStyle()
        para.headIndent = 00
        para.firstLineHeadIndent = 00
        para.tailIndent = 0
        para.lineBreakMode = .ByWordWrapping
        para.alignment = .Center
        para.paragraphSpacing = 0
        myMutableString.addAttribute(
            NSParagraphStyleAttributeName,
            value:para, range:NSMakeRange(0,1))
        return myMutableString
    }
    

    along with the following to use this function:

        let hintSize = CGFloat(80.0)
        let hintLabel = ASAttributedLabelNode(size:CGSizeMake(playableRect.size.width*0.9, hintSize))
    
        hintLabel.attributedString = outlinedCenteredString("Touch the Rope to Release the Wood", size: hintSize)
    
        hintLabel.position =
            CGPointMake(
                size.width/2.0,
                ((size.height - playableRect.size.height)/2.0) + hintSize/2.0
        )
    
        hintLabel.zPosition = kHintZPosition
        addChild(hintLabel)
    

提交回复
热议问题