suzuki-0000 / CountdownLabel

Simple countdown UILabel with morphing animation, and some useful function.
MIT License
999 stars 192 forks source link

Label text stroke #11

Open otymartin opened 8 years ago

otymartin commented 8 years ago

@suzuki-0000 I want to add a stroke to the timer with NSMutableAttributedString however it wont work

let attribs = [
            NSStrokeColorAttributeName: UIColor.blackColor(), NSForegroundColorAttributeName: UIColor.yellowColor(),
            NSStrokeWidthAttributeName: 2.0
        ]

        self.countdownLabel.attributedText = NSAttributedString(string: self.countdownLabel.text, attributes: attribs)
suzuki-0000 commented 8 years ago

hi. how about morphingEnabled = false in your countdownlabel?

otymartin commented 8 years ago

@suzuki-0000 Didnt work. For some reason it would only work on the first countdown time then stop working the rest of the way. this is my embarassing solution

struct CountDownStroke {

    let textFontAttributes = [
        NSFontAttributeName : UIFont.time(),
        NSForegroundColorAttributeName: UIColor.whiteColor(),
        NSStrokeColorAttributeName: UIColor.blackColor().colorWithAlphaComponent(0.5),
        NSStrokeWidthAttributeName: -0.5
    ]

    let redFontAttributes = [
        NSFontAttributeName : UIFont.time(),
        NSForegroundColorAttributeName: UIColor.redColor().colorWithAlphaComponent(0.8),
        NSStrokeColorAttributeName: UIColor.blackColor().colorWithAlphaComponent(0.5),
        NSStrokeWidthAttributeName: -0.5
    ]

    func loadAttributes(countdownLabel: CountdownLabel) {

        countdownLabel.setCountDownTime(15)
        countdownLabel.timeFormat = "ss"

        dispatch(queue: .main) {
            countdownLabel.attributedText = NSAttributedString(string: countdownLabel.text, attributes: self.textFontAttributes)
        }

        countdownLabel.then(15) {
            dispatch(queue: .main) {
                countdownLabel.hidden = false
                countdownLabel.attributedText = NSAttributedString(string: countdownLabel.text, attributes: self.textFontAttributes)
            }
        }
        countdownLabel.then(14) {
            dispatch(queue: .main) {
                countdownLabel.attributedText = NSAttributedString(string: countdownLabel.text, attributes: self.textFontAttributes)
            }
        }
        countdownLabel.then(13) {
            dispatch(queue: .main) {
                countdownLabel.attributedText = NSAttributedString(string: countdownLabel.text, attributes: self.textFontAttributes)
            }
        }
        countdownLabel.then(12) {
            dispatch(queue: .main) {
                countdownLabel.attributedText = NSAttributedString(string: countdownLabel.text, attributes: self.textFontAttributes)
            }
        }
        countdownLabel.then(11) {
            dispatch(queue: .main) {
                countdownLabel.attributedText = NSAttributedString(string: countdownLabel.text, attributes: self.textFontAttributes)
            }
        }
        countdownLabel.then(10) {
            dispatch(queue: .main) {
                countdownLabel.timeFormat = "s"
                countdownLabel.attributedText = NSAttributedString(string: countdownLabel.text, attributes: self.textFontAttributes)
            }
        }
        countdownLabel.then(9) {
            dispatch(queue: .main) {
                countdownLabel.attributedText = NSAttributedString(string: countdownLabel.text, attributes: self.textFontAttributes)
            }
        }
        countdownLabel.then(8) {
            dispatch(queue: .main) {
                countdownLabel.attributedText = NSAttributedString(string: countdownLabel.text, attributes: self.textFontAttributes)
            }
        }
        countdownLabel.then(7) {
            dispatch(queue: .main) {
                countdownLabel.attributedText = NSAttributedString(string: countdownLabel.text, attributes: self.textFontAttributes)
            }
        }
        countdownLabel.then(6) {
            dispatch(queue: .main) {
                countdownLabel.textColor = UIColor.redColor()
                countdownLabel.attributedText = NSAttributedString(string: countdownLabel.text, attributes: self.textFontAttributes)
            }
        }
        countdownLabel.then(5) {
            dispatch(queue: .main) {
                countdownLabel.textColor = UIColor.redColor()
                countdownLabel.attributedText = NSAttributedString(string: countdownLabel.text, attributes: self.redFontAttributes)
            }
        }
        countdownLabel.then(4) {
            dispatch(queue: .main) {
                countdownLabel.attributedText = NSAttributedString(string: countdownLabel.text, attributes: self.redFontAttributes)
            }
        }
        countdownLabel.then(3) {
            dispatch(queue: .main) {
                countdownLabel.attributedText = NSAttributedString(string: countdownLabel.text, attributes: self.redFontAttributes)
            }
        }
        countdownLabel.then(2) {
            dispatch(queue: .main) {
                countdownLabel.attributedText = NSAttributedString(string: countdownLabel.text, attributes: self.redFontAttributes)
            }
        }
        countdownLabel.then(1) {
            dispatch(queue: .main) {
                countdownLabel.attributedText = NSAttributedString(string: countdownLabel.text, attributes: self.redFontAttributes)
            }
        }
    }
}
suzuki-0000 commented 8 years ago

you want to do something like this? I found a bug in CountdownAttributedText test

otymartin commented 8 years ago

@suzuki-0000 Hi like this.

private var textAttributes: [String: NSObject] {
    let textAttributes = [
        NSForegroundColorAttributeName: UIColor.whiteColor(),
        NSStrokeColorAttributeName: UIColor.blackColor(),
        NSFontAttributeName: UIFont.navigation(),
        NSStrokeWidthAttributeName: -1.5
    ]
    return textAttributes
}

 self.countdownLabel.attributedText = NSAttributedString(string: self.countdownLabel.text, attributes: attributes)

//This currently does not work