Uicountinglabel as the effect of text animation, is the most used in OC, today we talk about basic use in swift, including swift3 and swift4 How to use Uicountinglabel
want to use Uicountinglabel or to download it first .
Download Address: Https://github.com/dataxpress/UICountingLabel
Uicountinglabel only supports shaping and floating-point style because this is written with OC, we need to use the Swift Bridge to connect the Austrian, will not bridge the students can self-Baidu many tutorials We first look at the effect and code in the SWIFT3
to sort out some basic
1. Create (Don't say more, you can create it by init)
2. How to use the above image under Rich text
on the Code
var titleText: String! {
didSet {
//e.g: Take the string "10239" as an example
// subbStrs is to intercept 10239 in the current string
let subbStrs = titleText? .substring (to: (titleText? .index ((titleText? .startIndex) !, offsetBy: (titleText? .count)!-1))!)
let str = titleText
let index = str? .index ((str? .startIndex) !, offsetBy: (str? .count)!-1)
// subStr is the number of intercepted strings
let subStr = str? .substring (from: index!)
self.subStr = subStr
// UICountingLabel's rich text callback
countingLabel.attributedFormatBlock = ((_ value) in
let object = "\ (Int (value)) \ (self.subStr!)"
let attributedStr = NSMutableAttributedString.init (string: object)
let rangeStr = object as NSString
let range = rangeStr.range (of: self.subStr!)
// Swift3 gets the specified string according to the current range, and then changes the size
attributedStr.addAttribute (NSFontAttributeName, value: UIFont.pingfangSC (size: 14), range: range)
return attributedStr
}
// The following is the basic configuration UICountingLabel animation effect, just two sentences
countingLabel.format = "% d"
countingLabel.count (from: 0, to: CGFloat (Int (subbStrs!)!), withDuration: 2)
}
}
var subStr: String!
We're looking at the use of the SWIFT4.
The effect is the same, that is, the syntax of this piece is a little different
countingLabel.attributedFormatBlock = ((_ object) in
let str = "\ (Int (object))%"
let attrStr = NSMutableAttributedString.init (string: str)
let rangeStr = str as NSString
let range = rangeStr.range (of: "%")
// The rich text syntax system has made some changes, and it looks OK
attrStr.addAttribute (NSAttributedStringKey.font, value: UIFont.systemFont (ofSize: 30), range: range)
attrStr.addAttribute (NSAttributedStringKey.foregroundColor, value: UIColor.red, range: range)
return attrStr
}
countingLabel.format = "% d"
countingLabel.count (from: 0, to: 10239, withDuration: 2)
The above is swift3 and swift4 in Uicountinglabel Rich Text basic use, have any questions can be in the bottom of the message Austria