mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			57 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Swift
		
	
		
		
			
		
	
	
			57 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Swift
		
	
| 
											8 years ago
										 | // | ||
| 
											8 years ago
										 | //  Copyright (c) 2018 Open Whisper Systems. All rights reserved. | ||
| 
											8 years ago
										 | // | ||
|  | 
 | ||
|  | import Foundation | ||
|  | import UIKit | ||
|  | 
 | ||
| 
											8 years ago
										 | @objc public class TextFieldHelper: NSObject { | ||
| 
											8 years ago
										 | 
 | ||
|  |     // Used to implement the UITextFieldDelegate method: `textField:shouldChangeCharactersInRange:replacementString` | ||
|  |     // Takes advantage of Swift's superior unicode handling to append partial pasted text without splitting multi-byte characters. | ||
| 
											8 years ago
										 |     @objc public class func textField(_ textField: UITextField, shouldChangeCharactersInRange editingRange: NSRange, replacementString: String, byteLimit: UInt) -> Bool { | ||
| 
											8 years ago
										 | 
 | ||
|  |         let byteLength = { (string: String) -> UInt in | ||
|  |             return UInt(string.utf8.count) | ||
|  |         } | ||
|  | 
 | ||
|  |         let existingString = textField.text ?? "" | ||
|  | 
 | ||
|  |         // Given an NSRange, we need to interact with the NS flavor of substring | ||
|  |         let removedString = (existingString as NSString).substring(with: editingRange) | ||
|  | 
 | ||
|  |         let lengthOfRemainingExistingString = byteLength(existingString) - byteLength(removedString) | ||
|  | 
 | ||
|  |         let newLength = lengthOfRemainingExistingString + byteLength(replacementString) | ||
|  | 
 | ||
|  |         if (newLength <= byteLimit) { | ||
|  |             return true | ||
|  |         } | ||
|  | 
 | ||
|  |         // Don't allow any change if inserting a single char is already over the limit (typically this means typing) | ||
| 
											8 years ago
										 |         if (replacementString.count < 2) { | ||
| 
											8 years ago
										 |             return false | ||
|  |         } | ||
|  | 
 | ||
|  |         // However if pasting, accept as much of the string as possible. | ||
|  |         let availableSpace = byteLimit - lengthOfRemainingExistingString | ||
|  | 
 | ||
|  |         var acceptableSubstring = "" | ||
|  | 
 | ||
| 
											8 years ago
										 |         for (_, char) in replacementString.enumerated() { | ||
| 
											8 years ago
										 |             var maybeAcceptableSubstring = acceptableSubstring | ||
|  |             maybeAcceptableSubstring.append(char) | ||
|  |             if (byteLength(maybeAcceptableSubstring) <= availableSpace) { | ||
|  |                 acceptableSubstring = maybeAcceptableSubstring | ||
|  |             } else { | ||
|  |                 break | ||
|  |             } | ||
|  |         } | ||
|  | 
 | ||
| 
											8 years ago
										 |         textField.text = (existingString as NSString).replacingCharacters(in: editingRange, with: acceptableSubstring) | ||
| 
											8 years ago
										 | 
 | ||
|  |         // We've already handled any valid editing manually, so prevent further changes. | ||
|  |         return false | ||
|  |     } | ||
|  | } |