简体   繁体   中英

UITextView - InsertText doesn't auto-correct or auto-capitalize

I have a custom InputView on a UITextView with auto-correct and auto-capitalization turned on.

My custom input view has a bunch of UIButtons that call InsertText on the UITextView . This works fine for inserting text at the current cursor position. The problem is that auto-correction and auto-capitalization do not work when InsertText is called. If input comes in through a bluetooth keyboard, or the standard keyboard, everything works fine.

Is there some method I can call on UITextView to invalidate auto-correct/auto-capitalization? SetNeedsDisplay was my first attempt, but it had no effect. Also, my app may potentially be iOS 7 and higher, so it's fine to use newer text APIs.

*NOTE: I'm using MonoTouch (Xamarin.iOS), but Objective-C answers are welcome.

Not without rolling your own autocorrect system. The iOS autocorrect system is opaque to developers, and only works on keyboard input. Any time you explicitly set the .text property on an input or text view, the value you set is what will appear.

Ok, I found a way in which this works:

  • Implement your own NSTextStorage
  • Setup an NSLayoutManager and NSTextContainer to coordinate your NSTextStorage with UITextView
  • When programmatically modifying your NSTextStorage subclass, auto correct "just works"

I think this works because modifying NSTextStorage fires events that notify the UITextView to update things like autocorrect. It works only on iOS 7 and higher.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM