Commit c50a53e0 by Sarkizz

添加一个与字符串做比较的协议

parent ee3d36b9
...@@ -128,6 +128,7 @@ extension String { ...@@ -128,6 +128,7 @@ extension String {
} }
} }
// MARK: - Calculate text size
extension String { extension String {
public func size(with attributes: MRStringAttribute ..., fixedWidth: CGFloat) -> CGSize { public func size(with attributes: MRStringAttribute ..., fixedWidth: CGFloat) -> CGSize {
guard count > 0 && fixedWidth > 0 else { guard count > 0 && fixedWidth > 0 else {
...@@ -157,3 +158,39 @@ extension String { ...@@ -157,3 +158,39 @@ extension String {
return DataURLParser<UIImage>.parserDataURL(self) return DataURLParser<UIImage>.parserDataURL(self)
} }
} }
// MARK: - Compare with MRCompareToString
extension String {
public static func == <T: MRCompareToString>(lhs: Self, rhs: T) -> Bool {
return rhs.compare(with: lhs) == .orderedSame
}
public static func < <T: MRCompareToString>(lhs: Self, rhs: T) -> Bool {
return rhs.compare(with: lhs) == .orderedDescending
}
public static func <= <T: MRCompareToString>(lhs: Self, rhs: T) -> Bool {
let rs = rhs.compare(with: lhs)
return rs == .orderedSame || rs == .orderedDescending
}
public static func > <T: MRCompareToString>(lhs: Self, rhs: T) -> Bool {
return rhs.compare(with: lhs) == .orderedAscending
}
public static func >= <T: MRCompareToString>(lhs: Self, rhs: T) -> Bool {
let rs = rhs.compare(with: lhs)
return rs == .orderedSame || rs == .orderedAscending
}
}
/// 比较字符串的协议。任意类遵守该协议来实现跟字符串比较
public protocol MRCompareToString: Comparable {
static func < (lhs: Self, rhs: String) -> Bool
static func <= (lhs: Self, rhs: String) -> Bool
static func > (lhs: Self, rhs: String) -> Bool
static func >= (lhs: Self, rhs: String) -> Bool
static func == (lhs: Self, rhs: String) -> Bool
func compare(with string: String) -> ComparisonResult
}
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