2014-12-08 4 views
3

선행 공백을 허용하지 않으려는 textField가 있습니다. 그래서 textField(textField:shouldChangeCharactersInRange:replacementString:)을 구현하고 텍스트를 공백으로 시작하는 것으로 변경하려는 시도를 차단합니다. 이것은 예상대로 작동합니다.QuickType 예측에서 UITextFieldDelegate에 의해 차단되어야하는 키 스트로크를받습니다.

불행히도 이것은 퀵타임을 엉망으로 만듭니다. 빈 필드에서 공백을 누를 때마다 내 textField에서 무시할 때마다 Quicktype 텍스트 앞에이 공백이옵니다. 더 명확하게 말하면, QuickType이 삽입 할 텍스트 앞에 접두사가 붙습니다. 추가 공백은 QuickType 막대의 UI에 표시되지 않습니다. 여기

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { 
    let currentText = textField.text as NSString 
    let proposedText = currentText.stringByReplacingCharactersInRange(range, withString: string) 
    if !proposedText.isEmpty { 
     let firstCharacterString = String(proposedText[proposedText.startIndex]) as NSString 
     if firstCharacterString.rangeOfCharacterFromSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).location == 0 { 
      println("starts with whitespace \"\(proposedText)\"") 
      return false 
     } 
    } 
    return true 
} 

는 I 공간 께 I'm QuickType 제안 눌러 다음 5 번 누르면, 일부 기록이 일어나고 있는지 확인할 수있다 :

starts with whitespace " "   // pressing space key 
starts with whitespace " " 
starts with whitespace " " 
starts with whitespace " " 
starts with whitespace " " 
starts with whitespace "  I'm" // Inserted by QuickType after pressing the "I'm" suggestion 
starts with whitespace " "   // Inserted by QuickType as well 

를 I는 것을 확인할 수 위임 방법의 변수를 검사함으로써 문제는 실제로 UITextField에서 가져 오는 대체 문자열입니다. 그것은 공백으로 시작되는 제안을 이미 포함하고 있습니다.

누구나 내가 그것을 막을 수 있는지 또는 어떻게 QuickType 제안을 "재설정"할 수 있는지 알고 있습니까?

해결 방법은 여러 문자 삽입에서 공백을 제거하는 것이지만 처음에는 문제를 깨끗하게 처리 할 방법이 없는지 확인하고 싶습니다.

답변

0

더 많은 테스트를 통해 이것이 버그라는 결론에 도달했습니다.

처음에는 Keyboard와 QuickType이 UITextField에서 분리되어 있다고 생각했습니다. 그러나 실제로는 그렇지 않습니다. 채워진 textField에서 커서의 위치를 ​​변경하면 실제로 quicktype 제안이 변경됩니다. 따라서 textField는 실제로 quicktype과 통신합니다.

그래서 버그를 제출했습니다. rdar : 애플

버그 OpenRadar에서 // 19250739
버그 :

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { 
    let currentText = textField.text as NSString 
    let proposedText = currentText.stringByReplacingCharactersInRange(range, withString: string) 

    if proposedText.hasPrefix(" ") { 
     // Don't allow space at beginning 
     println("Ignore text \"\(proposedText)\"") 

     // workaround 
     if textField.text == "" && countElements(string) > 1 { 
      // multi insert into empty field. probably from QuickType 
      // so we should strip whitespace and set new text directly 
      let trimmedString = proposedText.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) 
      println("Inserted \"\(trimmedString)\" with Quicktype. ") 
      textField.text = trimmedString 
     } 

     return false 
    } 
    return true 
} 
: 경우 누군가에 5794406481264640

는 해결에 관심

관련 문제