Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift version #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions MJAlertView-Swift/CGFloatExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// CGFloatExtension.swift
// ChildrenGuardian
//
// Created by Jason.Chengzi on 16/06/17.
// Copyright © 2016年 Hvit. All rights reserved.
//

extension CGFloat {
///转换为Float
var toFloat : Float { return Float(self) }
///转换为Int
var toInt : Int { return Int(self) }
///转换为Double
var toDouble : Double { return Double(self) }

///一半数值
var half : CGFloat { return self / 2 }
///相反数值
var opposite : CGFloat { return -self }
///绝对值
var absolute : CGFloat { return abs(self) }
///两倍数值
var double : CGFloat { return self * 2 }
///三倍数值
var triple : CGFloat { return self * 3 }
///平方数
var square : CGFloat { return (self * self) }
///立方数
var cube : CGFloat { return (self * self * self) }

static func MIN(lbs lbs : CGFloat, rbs : CGFloat) -> CGFloat {
if lbs > rbs {
return rbs
} else if lbs < rbs {
return lbs
} else {
return lbs
}
}
}
125 changes: 125 additions & 0 deletions MJAlertView-Swift/NumberExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
//
// NumberExtension.swift
// HealthPlus
//
// Created by Jason.Chengzi on 16/05/20.
// Copyright © 2016年 Hvit. All rights reserved.
//
import UIKit

extension Int {
///转换为CGFloat
var toCGFloat : CGFloat { return CGFloat(self) }
///转换为Float
var toFloat : Float { return Float(self) }
///转换为Double
var toDouble : Double { return Double(self) }

///一半数值
var half : Int { return self / 2 }
///相反数值
var opposite : Int { return -self }
///绝对值
var absolute : Int { return abs(self) }
///两倍数值
var double : Int { return self * 2 }
///三倍数值
var triple : Int { return self * 3 }
///平方数
var square : Int { return (self * self) }
///立方数
var cube : Int { return (self * self * self) }

///是否在开区间内
func isGreaterThan(min : Int, lessThan max : Int) -> Bool {
if self > min && self < max {
return true
}
return false
}
///是否在闭区间内
func isEqualAndGreaterThan(min : Int, equalAndLessThan max : Int) -> Bool {
if self >= min && self <= max {
return true
}
return false
}
}

extension Float {
///转换为CGFloat
var toCGFloat : CGFloat { return CGFloat(self) }
///转换为Int
var toInt : Int { return Int(self) }
///转换为Double
var toDouble : Double { return Double(self) }

///一半数值
var half : Float { return self / 2 }
///相反数值
var opposite : Float { return -self }
///绝对值
var absolute : Float { return abs(self) }
///两倍数值
var double : Float { return self * 2 }
///三倍数值
var triple : Float { return self * 3 }
///平方数
var square : Float { return (self * self) }
///立方数
var cube : Float { return (self * self * self) }

///是否在开区间内
func isGreaterThan(min : Float, lessThan max : Float) -> Bool {
if self > min && self < max {
return true
}
return false
}
///是否在闭区间内
func isEqualAndGreaterThan(min : Float, equalAndLessThan max : Float) -> Bool {
if self >= min && self <= max {
return true
}
return false
}
}

extension Double {
///转换为CGFloat
var toCGFloat : CGFloat { return CGFloat(self) }
///转换为Float
var toFloat : Float { return Float(self) }
///转换为Int
var toInt : Int { return Int(self) }

///一半数值
var half : Double { return self / 2 }
///相反数值
var opposite : Double { return -self }
///绝对值
var absolute : Double { return abs(self) }
///两倍数值
var double : Double { return self * 2 }
///三倍数值
var triple : Double { return self * 3 }
///平方数
var square : Double { return (self * self) }
///立方数
var cube : Double { return (self * self * self) }

///是否在开区间内
func isGreaterThan(min : Double, lessThan max : Double) -> Bool {
if self > min && self < max {
return true
}
return false
}
///是否在闭区间内
func isEqualAndGreaterThan(min : Double, equalAndLessThan max : Double) -> Bool {
if self >= min && self <= max {
return true
}
return false
}
}
145 changes: 145 additions & 0 deletions MJAlertView-Swift/StringExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
//
// StringExtension.swift
// HealthPlus
//
// Created by Jason.Chengzi on 16/05/17.
// Copyright © 2016年 Hvit. All rights reserved.
//
import UIKit

extension String {
///获取NSString对象
var toNSString : NSString {
return NSString(string: self)
}
///获取字符串长度
var length : Int {
return self.characters.count
}

///判断是否为整数
var isInt : Bool {
if Int(self) != nil {
return true
} else {
return false
}
}
///判断是否为浮点数
var isFloat : Bool {
if Float(self) != nil {
return true
} else {
return false
}
}
///判断是否为长浮点数
var isDouble : Bool {
if Double(self) != nil {
return true
} else {
return false
}
}

///尝试转换为整型
var nullableInt : Int? {
return Int(self)
}
///尝试转换为浮点数
var nullableFloat : Float? {
return Float(self)
}
///尝试转换为长浮点数
var nullableDouble : Double? {
return Double(self)
}

///强制转换为整型,失败返回0
var toInt : Int {
return Int(self) ?? 0
}
///强制转换为浮点数,失败返回0
var toFloat : Float {
return Float(self) ?? Float(0)
}
///强制转换为长浮点数,失败返回0
var toDouble : Double {
return Double(self) ?? Double(0)
}

///获取第一个字母
var firstLetter : String {
var ret = ""
if !self.canBeConvertedToEncoding(NSASCIIStringEncoding) {
ret = self.letters.firstLetter
} else {
ret = "\("\(self.characters.first)" ?? "")"
}
return ret
}

var letters : String {
let str = NSMutableString(string: self)
if CFStringTransform(str, nil, kCFStringTransformMandarinLatin, false) {
if CFStringTransform(str, nil, kCFStringTransformStripDiacritics, false) {
return str as String
}
}
return self
}

func getFirstLetter() -> String {
return self.firstLetter
}

///通过字体获取字符串的尺寸
func sizeWithFont(font : UIFont) -> CGSize {
//如果没有内容
guard self.isEmpty == false else {
return CGSizeZero
}
return self.toNSString.sizeWithAttributes([NSFontAttributeName : font])
}
///通过字体和固定宽度计算字符串的高度
func heightWithWidth(width : CGFloat, font : UIFont) -> CGFloat {
//如果没有内容
guard self.isEmpty == false else {
return CGFloat(0)
}
return self.sizeWithFont(font).width / width
}
///截取指定长度的字符串
func substringWithStartPosition(start : Int, andEndPosition end : Int) -> String {
return self.substringWithRange(self.startIndex.advancedBy(start)..<self.startIndex.advancedBy(end))
}
///判断是否满足正则表达式
func isMatchingPattern(pattern : MCPattern) -> Bool {
let regex : NSRegularExpression? = try? NSRegularExpression(pattern: pattern.pattern, options: NSRegularExpressionOptions.CaseInsensitive)
if let matches = regex?.matchesInString(self, options: [], range: NSMakeRange(0, self.toNSString.length)) {
return matches.count > 0
} else {
return false
}
}

//MD5加密字符串
func md5() -> String {
let str = self.cStringUsingEncoding(NSUTF8StringEncoding)
let strLen = CUnsignedInt(self.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))
let digestLen = Int(CC_MD5_DIGEST_LENGTH)
let result = UnsafeMutablePointer<CUnsignedChar>.alloc(digestLen)
CC_MD5(str!, strLen, result)
let hash = NSMutableString()
for i in 0..<digestLen {
hash.appendFormat("%02x", result[i])
}
result.destroy()
return String(format: hash as String)
}

}

struct MCPattern {
var pattern : String
}
Loading