nubideus

Untitled

Jul 14th, 2018
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.70 KB | None | 0 0
  1. // uitextviewdelegate
  2.     private var changedTextRange: NSRange! = nil
  3.     private var replacementText: String! = nil
  4.    
  5.     func textViewDidChange(_ textView: UITextView) {
  6.         UpdateTextViewSize()
  7.         guard changedTextRange != nil, replacementText != nil else {
  8.             return
  9.         }
  10.         autocomplete.didChangeText(range: changedTextRange, replacementText: replacementText)
  11.         changedTextRange = nil
  12.         replacementText = nil
  13.     }
  14.    
  15.     func textViewDidChangeSelection(_ textView: UITextView) {
  16.         if changedTextRange == nil {
  17.             autocomplete.didChangeText(range: NSRange(location: textView.selectedRange.location, length: 0), replacementText: "")
  18.         }
  19.     }
  20.    
  21.     func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
  22.         /* if invalid newstring then return before next code */
  23.        
  24.         changedTextRange = range
  25.         replacementText = text
  26.        
  27.         return true
  28.     }
  29.  
  30. // autocomplete
  31.     var validInputStarted = false
  32.     var stringRange: Range<Int>! = nil
  33.  
  34.     func didChangeText(range: NSRange, replacementText text: String) {
  35.         let marker = "&" //
  36.         var autoString = ""
  37.         if validInputStarted {
  38.             switch stringRange.lowerBound <= range.lowerBound && range.lowerBound <= stringRange.upperBound {
  39.             case true:
  40.                 stringRange = stringRange.lowerBound..<(stringRange.upperBound + (text.count - range.length))
  41.                 autoString = String(Array(textView.text)[stringRange])
  42.                
  43.                 if autoString.range(of: "^[a-z0-9]*(\\.[a-z0-9])*$", options: .regularExpression) == nil {
  44.                     autoString = ""
  45.                     fallthrough
  46.                 }
  47.             default:
  48.                 validInputStarted = false
  49.                 stringRange = nil
  50.                 // clear()
  51.                 // очищаешь таблицу поиска и/или уничтожаешь еще
  52.             }
  53.         }
  54.         if !validInputStarted, range.lowerBound + text.count - marker.count >= 0 && text.count > 0 {
  55.             let markerRange = Range(range.lowerBound - marker.count + text.count..<range.lowerBound + text.count)
  56.             if String(Array(textView.text)[markerRange]) == marker {
  57.                 stringRange = markerRange.lowerBound + marker.count..<markerRange.upperBound
  58.                 validInputStarted = true
  59.                
  60.                 if let str = Optional((textView.text).suffix(textView.text.count - stringRange.lowerBound)),
  61.                     let range = str.range(of: "^[a-z0-9]*(\\.[a-z0-9])*", options: .regularExpression) {
  62.                    
  63.                     stringRange = NSRange(
  64.                         location: stringRange.lowerBound,
  65.                         length: str.distance(from: range.lowerBound, to: range.upperBound)
  66.                     ).toRange()!
  67.                     autoString = str.substring(with: range)
  68.                     print(stringRange)
  69.                 }
  70.                
  71.                 /* if (self.topConstraint?.isActive ?? true) == false { // show gui
  72.                     UIView.animate(withDuration: 0.3, animations: {
  73.                         self.topConstraint?.isActive = true
  74.                         self.layoutIfNeeded()
  75.                     })
  76.                 } */ // таблицу можешь показывать тут
  77.             }
  78.         }
  79.        
  80.         guard validInputStarted else {
  81.             return
  82.         }
  83.        
  84.         print(autoString) // готовая строка
  85.         // делаешь запросы и отображаешь в таблице
  86.         return;
  87.     }
Add Comment
Please, Sign In to add comment