项目中的一些常用公共方法 - swift3.1

2017-05-19  本文已影响0人  JoyGY

1. 数据显示格式的转换 - 千分位分隔符显示数据

class func thousandsOfSub_spacesForNumber(numStr: String?) -> String{
        if numStr != nil && numStr != "<null>" && numStr != ""  && numStr != "-"{

            let num = Double(numStr!)!

            if num < 1000 || num < -1000{

                let changeNum = String(format: "%.2f", num)
                return changeNum
            }else {
                let numberFormatter = NumberFormatter()
                numberFormatter.positiveFormat = ",###.00;"

                let changeNum = numberFormatter.string(from: NSNumber(value: num))
                return changeNum!
            }
        }
        
        return "0.00"
    }

2. 将字符串替换*号 - 隐藏显示的数据

    /**
     - parameter str:        字符
     - parameter startindex: 开始字符索引
     - parameter endindex:   结束字符索引

     - returns: 替换后的字符串
    */
    class func stringByX(_ str:String,startindex:Int,endindex:Int) -> String{
        //开始字符索引
        let startIndex = str.characters.index(str.startIndex, offsetBy: startindex)
        //结束字符索引
        let endIndex = str.characters.index(str.startIndex, offsetBy: endindex)
        let range = Range<String.Index>(startIndex..<endIndex)
        var s = String()
        for _ in 0..<endindex - startindex{
            s += "*"
        }
        return str.replacingCharacters(in: range, with: s)
    }

3. 获取随机颜色

    class func randomColorShow() -> UIColor{
        let red = Double(arc4random_uniform(256))
        let green = Double(arc4random_uniform(256))
        let blue = Double(arc4random_uniform(256))

        let color = UIColor(red: CGFloat(red/255.0), green: CGFloat(green/255.0), blue: CGFloat(blue/255.0), alpha: 1)
        return color
    }

4. 获取图片的尺寸大小

  class func getImagePicReallySize(_ img: UIImage) -> CGSize {
        // 找出图片与屏幕的宽高比例,依照先到达为准
        var scale: CGFloat = 0;
        let scaleX = screenWidth / img.size.width
        let scaleY = screenHeight / img.size.height
        scale = (scaleX > scaleY) ? scaleY : scaleX

        // 按比例修改Size
        let size = CGSize(width: img.size.width * scale, height: img.size.height * scale)
        return size
    }

5. 获取字符串的宽度和高度size 与 文字行数

    class func getTextReallyRectSize(_ text:NSString, font:UIFont, size:CGSize) -> CGRect {
        let attributes = [NSFontAttributeName:font]
        let option = NSStringDrawingOptions.usesLineFragmentOrigin
        let rect:CGRect = text.boundingRect(with: size, options: option, attributes: attributes, context: nil)
        return rect
    }

    /**获取文字的行数 */
    class func getHangNumberWithTextString(strW: CGFloat, lagerW: CGFloat) -> Int {

        var hang: Int = 1
        if strW.truncatingRemainder(dividingBy: lagerW) == 0.0 {
            hang = Int(strW / lagerW)
        }else {
            hang = Int(strW / lagerW + 1)
        }
        return hang
    }

6. 字符串截取

    class func stringBySeperate(_ words: String, start: Int, end: Int) -> String {
        if words == "<null>" {
            return "-"
        }
        let index = words.characters.index(words.startIndex, offsetBy: start)
        let index2 = words.characters.index(words.startIndex, offsetBy: end)
        let range = index ..< index2
        // Range<String.Index>(start: index, end: index2)
        let rangeStr = words.substring(with: range)
        return rangeStr
    }

7. 富文本设置数值与百分号

    class func settingPercentAndValueForDesign(_ textStr: String, textFont: UIFont, textLab: UILabel, textColor: UIColor) {


        let color = textColor

        let strLength = textStr.characters.count
        if strLength == 0 || textStr == "-"{
            textLab.text = "-"
        }else {
            let mutableAttrStr = NSMutableAttributedString(string: textStr)
            //8.54
            mutableAttrStr.addAttributes([NSFontAttributeName: textFont, NSForegroundColorAttributeName: color], range: NSMakeRange(0,strLength - 1))

            //%
            mutableAttrStr.addAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 13), NSForegroundColorAttributeName: color], range: NSMakeRange(strLength - 1,1))

            // 富文本显示lab的内容
            textLab.attributedText = mutableAttrStr
        }
    }

8. 获取拼音首字母(传入汉字字符串, 返回大写拼音首字母)

    class func getTheFirstLetterForCharacters(_ aString: String) -> String{
        //转成了可变字符串
        let str = NSMutableString(string: aString)
        //先转换为带声调的拼音
        CFStringTransform(str, nil, kCFStringTransformMandarinLatin, false)
        //再转换为不带声调的拼音
        CFStringTransform(str, nil, kCFStringTransformStripDiacritics, false)
        //转化为大写拼音
        let pinYin: NSString = str.capitalized as NSString
        //获取并返回首字母
        return pinYin.substring(to: 1)
    }

9.获取指定年月的开始和结束的日期

    //指定年月的开始日期 - //注意这个直接返回的date是0时区的,需要formate转换成字符串,获取当前时区的,才是正确的结果
    class func startOfMonth(year: Int, month: Int) -> NSDate {

        let calendar = NSCalendar.current
        let startComps = NSDateComponents()
        startComps.day = 1
        startComps.month = month
        startComps.year = year
        let startDate = calendar.date(from: startComps as DateComponents)!

        return startDate as NSDate
    }

    //指定年月的结束日期
    class func endOfMonth(year: Int, month: Int, returnEndTime:Bool = false) -> NSDate {
        let calendar = NSCalendar.current
        let components = NSDateComponents()
        components.month = 1
        if returnEndTime {
            components.second = -1
        } else {
            components.day = -1
        }

        let endOfYear = calendar.date(byAdding: components as DateComponents, to: startOfMonth(year: year, month: month) as Date)!
        return endOfYear as NSDate
    }

10. 字典转换成json - 请求时 传送参数-需要传递一个json字符串给服务器

    class func dictionaryChangeJSON(dic: NSDictionary) -> NSString {
        do {
            let tpDic = try JSONSerialization.data(withJSONObject: dic, options: JSONSerialization.WritingOptions.prettyPrinted)
            let changeStr = NSString(data: tpDic, encoding: String.Encoding.utf8.rawValue)
            return changeStr!
        } catch {
            fatalError("转换失败:\(error)")
        }
    }

11. 友好的显示日期格式

    class func friendlyTime(_ dateTime: String) -> String {

        //日期格式 yyyy年MM月dd日 HH:mm:ss
        let dateFormatter: DateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

        if let date = dateFormatter.date(from: dateTime) {
            let delta = Date().timeIntervalSince(date)//获得秒数
            if delta < 3600 { //一个小时内
                if delta <= 0 {
                    return Language.getStrings(name: time_just) //刚刚
                }else if (delta < 60) { //几秒以前
                    return "\(Int(delta)) "+Language.getStrings(name: time_SecondAgo)
                }else{ //几分钟以前
                    return "\(Int(delta / 60)) " + Language.getStrings(name: time_MinuteAgo)
                }
            }else if delta == 3600 { //1小时
                return "1 " + Language.getStrings(name: time_Hour)
            }else{
                let calendar = Calendar.current
                let unitFlags: NSCalendar.Unit = [NSCalendar.Unit.year, NSCalendar.Unit.month, NSCalendar.Unit.day, NSCalendar.Unit.hour, NSCalendar.Unit.minute, NSCalendar.Unit.second]
                let comp = (calendar as NSCalendar).components(unitFlags,from:Date())
                let currentYear = comp.year! 
                let currentMonth = comp.month! 
                let currentDay = comp.day! 
                let currentHour = comp.hour!

                let compSec = (calendar as NSCalendar).components(unitFlags,from: date)
                let year = compSec.year! 
                let month = compSec.month! 
                let day = compSec.day! 
                let hour = compSec.hour! 
                var minute = compSec.minute! 

                //设置分为两位 16:7--16:07
                if compSec.minute! < 10 {
                    let temp = "0\(minute)"
                    minute = Int(temp)!
                }

                //检查是不是在同一天
                if currentYear == year && currentMonth == month && currentDay == day {
                    let divHour = currentHour - hour
                    return "\(divHour) " + Language.getStrings(name: time_HourAgo)
                }else{//不在同一天
                    if currentYear == year && currentMonth == month {
                        let divDay = currentDay - day
                        if divDay == 1 {
                            return Language.getStrings(name: time_Yesterday) + " \(hour):\(minute)"
                        }else{
                            return "\(year)-\(month)-\(day) \(hour):\(minute)"
                        }
                    }else{
                        let divMonth = currentMonth - month
                        if currentDay == 1 && (divMonth == 1 || (currentMonth == 1 && month == 12)){
                            return Language.getStrings(name: time_Yesterday) + " \(hour):\(minute)"
                        }else{
                            return "\(year)-\(month)-\(day) \(hour):\(minute)"
                        }
                    }
                }
            }
        }
        return ""
    }

12. 绘制左上角和右上角的两个圆角

/** 绘制左上角和右上角的两个圆角 */
    class func drawTopCorner(rect: CGRect, vi: UIView, borderColor: UIColor) {
        //切左上右上圆角
        let maskPath = UIBezierPath(roundedRect:vi.bounds, byRoundingCorners: [.topLeft,.topRight], cornerRadii: CGSize(width: 5.0, height: 5.0))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = vi.bounds
        maskLayer.path = maskPath.cgPath
        vi.layer.mask = maskLayer

        //添加border
        let borderLayer = CAShapeLayer()
        borderLayer.frame = vi.bounds
        borderLayer.path = maskPath.cgPath
        borderLayer.lineWidth = 0.5
        borderLayer.strokeColor = borderColor.cgColor
        borderLayer.fillColor = UIColor.clear.cgColor

        let layers:NSArray = vi.layer.sublayers! as NSArray

        if (((layers.lastObject! as! CALayer).isKind(of: CAShapeLayer.self))) {
            (layers.lastObject as AnyObject).removeFromSuperlayer()
        }
        vi.layer.addSublayer(borderLayer)
    }

13. 绘制左下角和右下角的两个圆角

/** 绘制左下角和右下角的两个圆角 */
    class func drawBottomCorner(rect: CGRect, vi: UIView, borderColor: UIColor) {
        //切左下右下圆角
        let maskPath = UIBezierPath(roundedRect:vi.bounds, byRoundingCorners:[.bottomLeft,.bottomRight], cornerRadii: CGSize(width: 5.0, height: 5.0))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = vi.bounds
        maskLayer.path = maskPath.cgPath
        vi.layer.mask = maskLayer


        //添加border
        let borderLayer = CAShapeLayer()
        borderLayer.frame = vi.bounds
        borderLayer.path = maskPath.cgPath
        borderLayer.lineWidth = 0.5
        borderLayer.strokeColor = borderColor.cgColor
        borderLayer.fillColor = UIColor.clear.cgColor

        let layers:NSArray = vi.layer.sublayers! as NSArray

        if (((layers.lastObject! as! CALayer).isKind(of: CAShapeLayer.self))) {
            (layers.lastObject as AnyObject).removeFromSuperlayer()
        }
        vi.layer.addSublayer(borderLayer)
    }
上一篇下一篇

猜你喜欢

热点阅读