Swift-uicountinglabel basic use of rich text

Source: Internet
Author: User
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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.