Commit 246f3793 by Sarkizz

添加扩展API

parent a33c0e4a
...@@ -41,6 +41,69 @@ extension Date { ...@@ -41,6 +41,69 @@ extension Date {
} }
} }
extension Date {
public var year: Int {
return Calendar.current.component(.year, from: self)
}
public var month: Int {
return Calendar.current.component(.month, from: self)
}
public var week: Int {
return Calendar.current.component(.weekOfYear, from: self)
}
public var weekday: Int {
return Calendar.current.component(.weekday, from: self)
}
public var day: Int {
return Calendar.current.component(.day, from: self)
}
public var hour: Int {
return Calendar.current.component(.hour, from: self)
}
public var minute: Int {
return Calendar.current.component(.minute, from: self)
}
public var second: Int {
return Calendar.current.component(.second, from: self)
}
public var nanosecond: Int {
return Calendar.current.component(.nanosecond, from: self)
}
}
extension Date {
static let dayInSecond = 86400
public var torrmow: Date {
return after(1)
}
public var yesterday: Date {
return before(1)
}
public func after(_ days: Int) -> Date {
return offset(TimeInterval(days * Date.dayInSecond))
}
public func before(_ days: Int) -> Date {
return offset(-TimeInterval(days * Date.dayInSecond))
}
public func offset(_ interval: TimeInterval) -> Date {
let timeInterval = timeIntervalSinceReferenceDate + interval
return timeInterval.date()
}
}
extension TimeInterval { extension TimeInterval {
func date(_ type: TIConvertDateType = .reference) -> Date { func date(_ type: TIConvertDateType = .reference) -> Date {
switch type { switch type {
......
...@@ -20,3 +20,131 @@ extension String { ...@@ -20,3 +20,131 @@ extension String {
return hash.map { String(format: "%02x", $0) }.joined() return hash.map { String(format: "%02x", $0) }.joined()
} }
} }
struct Constants {
public static let moblieNumberRegex = "^1\\d{10}$"
public static let emailRegex = "^[A-Za-z0-9._%+-\\u4e00-\\u9fa5]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$"
public static let phoneNumberRegex = "^\\d{3,4}-?\\d{4,8}$"
}
extension String {
/// 是否电话号码(固话+手机号码)
public var isPhoneNumber: Bool {
return matches(Constants.phoneNumberRegex) || isMobileNumber
}
/// 是否手机号码
public var isMobileNumber: Bool {
return matches(Constants.phoneNumberRegex)
}
/// 是否邮箱
public var isEmail: Bool {
return matches(Constants.emailRegex)
}
/// 使用predicate匹配正则。
///
/// - Parameter regex: 正则
/// - Returns: 是否匹配
public func matches(_ regex: String) -> Bool {
return mrPredicate(regex).evaluate(with: self)
}
public func call() -> Bool {
if isPhoneNumber, let url = URL(string: "telprompt:\(self)"), UIApplication.shared.canOpenURL(url) {
return UIApplication.shared.openURL(url)
}
return false
}
private func mrPredicate(_ format: String) -> NSPredicate {
return NSPredicate(format: "SELF MATCHES %@", format)
}
}
public enum MRStringAttribute {
case font(UIFont)
case foregroundColor(UIColor)
case backgroundColor(UIColor)
case paragraphStyle(NSParagraphStyle)
case baselineOffset(Float)
case strikethroughColor(UIColor)
case strikethroughStyle(Int)
case ligature(Int)
case kern(Float)
case underlineStyle(NSUnderlineStyle)
case underlineColor(UIColor)
case link(NSURL)
}
extension String {
public func attribute(_ attrs: MRStringAttribute ..., range: NSRange? = nil) -> NSMutableAttributedString {
return attribute(attrs, range: range)
}
private func attribute(_ attrs: [MRStringAttribute], range: NSRange? = nil) -> NSMutableAttributedString {
let range = range ?? NSMakeRange(0, self.count)
let rs = NSMutableAttributedString(string: self)
attrs.forEach({ att in
switch att {
case .font(let font):
rs.addAttribute(.font, value: font, range: range)
break
case .foregroundColor(let color):
rs.addAttribute(.foregroundColor, value: color, range: range)
break
case .paragraphStyle(let paraStyle):
rs.addAttribute(.paragraphStyle, value: paraStyle, range: range)
break
case .backgroundColor(let color):
rs.addAttribute(.backgroundColor, value: color, range: range)
break
case .baselineOffset(let offset):
rs.addAttribute(.baselineOffset, value: offset, range: range)
break
case .strikethroughColor(let color):
rs.addAttribute(.strikethroughColor, value: color, range: range)
break
case .strikethroughStyle(let style):
rs.addAttribute(.strikethroughStyle, value: style, range: range)
break
case .ligature(let value):
rs.addAttribute(.ligature, value: value, range: range)
break
case .kern(let value):
rs.addAttribute(.kern, value: value, range: range)
break
case .underlineStyle(let style):
rs.addAttribute(.underlineStyle, value: style, range: range)
break
case .underlineColor(let color):
rs.addAttribute(.underlineColor, value: color, range: range)
break
case .link(let url):
rs.addAttribute(.link, value: url, range: range)
break
}
})
return rs
}
}
extension String {
public func size(with attributes:MRStringAttribute ..., fixedWidth: CGFloat) -> CGSize {
guard count > 0 && fixedWidth > 0 else {
return CGSize.zero
}
let fixedSize = CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude)
let text = attribute(attributes)
return text.boundingRect(with: fixedSize, options: .usesLineFragmentOrigin, context: nil).size
}
public func size(with font: UIFont, fixedWidth: CGFloat) -> CGSize {
guard count > 0 && fixedWidth > 0 else {
return CGSize.zero
}
let fixedSize = CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude)
let text = NSString(string: self)
return text.boundingRect(with: fixedSize, options: .usesLineFragmentOrigin, attributes: [.font: font], context: nil).size
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment