UITextView selectedRange not displaying when set programmatically

爱⌒轻易说出口 提交于 2019-11-30 05:01:36

Before setting the selection programmatically on the textView, just call select to make it show the new selection visually. For example:

[textView select:self];
textView.selectedRange = newSelectedRange; 

I'll answer my own question for now and update it later when there's more info (or if someone else finds a suitable work around, I'll happily move the check to their answer). I've got a bug report into apple and after submitting sample code, it's sitting there with open status. I'm pretty confident it's a bug, so when I get a response from them, I'll pass along here.

Suddenly meet this question. I've had the same problem until I check NSObject's declare header. They 've already provided a function named selectAll:

@interface NSObject(UIResponderStandardEditActions)   // these methods are not implemented in NSObject

- (void)cut:(id)sender __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
- (void)copy:(id)sender __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
- (void)paste:(id)sender __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
- (void)select:(id)sender __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
- (void)selectAll:(id)sender __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
- (void)delete:(id)sender __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_2);

@end
Andiih

Workaround: What you seem to want is UITextView placeholder functionality. This of course does not exist. However, there are some workarounds here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!