123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- //
- // KMBaseTextFormatter.swift
- // PDF Reader Pro
- //
- // Created by lizhe on 2022/12/12.
- //
- import Cocoa
- class KMBaseTextFormatter: NumberFormatter {
- var model: KMBaseTextFieldModel = KMBaseTextFieldModel()
- override func isPartialStringValid(_ partialStringPtr: AutoreleasingUnsafeMutablePointer<NSString>, proposedSelectedRange proposedSelRangePtr: NSRangePointer?, originalString origString: String, originalSelectedRange origSelRange: NSRange, errorDescription error: AutoreleasingUnsafeMutablePointer<NSString?>?) -> Bool {
- let data = self.model
- var partialString = partialStringPtr.pointee
- if partialString.debugDescription.isEmpty {
- return true
- }
- if (data.onlyNumber) {
- var regex: NSString = "^[0123456789.]*$"
- if !data.specialChart.debugDescription.isEmpty {
- regex = NSString(format: "^[0123456789%@]*$", data.specialChart)
- }
- let predicate = NSPredicate.init(format: "SELF MATCHES %@", regex)
- if !predicate.evaluate(with: partialString) {
- return false
- }
-
-
- if !data.specialChart.debugDescription.isEmpty {
- partialString = partialString.replacingOccurrences(of: data.specialChart, with: "") as NSString
- }
- if (Int(partialString.debugDescription) ?? 0 < data.minValue ||
- Int(partialString.debugDescription) ?? 0 > data.maxValue) {
- return false
- }
- }
-
- let newRange: NSRange = proposedSelRangePtr!.pointee
- if (NSMaxRange(origSelRange) > NSMaxRange(newRange)) {
- return true
- }
- return true
- }
-
- override func attributedString(for obj: Any, withDefaultAttributes attrs: [NSAttributedString.Key : Any]? = nil) -> NSAttributedString? {
- return nil
- }
- }
- //数字类型,只允许输入数字跟百分号
- class KMCSTextFormatter: NumberFormatter {
-
- override func isPartialStringValid(_ partialStringPtr: AutoreleasingUnsafeMutablePointer<NSString>, proposedSelectedRange proposedSelRangePtr: NSRangePointer?, originalString origString: String, originalSelectedRange origSelRange: NSRange, errorDescription error: AutoreleasingUnsafeMutablePointer<NSString?>?) -> Bool {
-
- let partialString = partialStringPtr.pointee
- if partialString.debugDescription.isEmpty {
- return true
- }
-
- let regex: NSString = "^[0123456789%]*$"
-
- let predicate = NSPredicate.init(format: "SELF MATCHES %@", regex)
- if !predicate.evaluate(with: partialString) {
- return false
- }
-
- let newRange: NSRange = proposedSelRangePtr!.pointee
- if (NSMaxRange(origSelRange) > NSMaxRange(newRange)) {
- return true
- }
- return true
- }
-
- override func attributedString(for obj: Any, withDefaultAttributes attrs: [NSAttributedString.Key : Any]? = nil) -> NSAttributedString? {
- return nil
- }
- }
|