简体   繁体   English

React Native TextInput抓取突出显示的文本,并在单击时保持突出显示

[英]React Native TextInput grabbing highlighted text, and keeping it highlighted when I click away

I have two things I want to do. 我有两件事想做。

  1. when I select text inside of a TextInput, and click on a button I would like to keep the text highlighted. 当我在TextInput内选择文本时,单击一个按钮,我想使文本突出显示。

  2. How can I grab the highlighted text from a TextInput? 如何从TextInput抓取突出显示的文本?

Shouldn't there be properties and events that can do this? 是否应该有可以做到这一点的属性和事件?

Since no one has answered I will give my solution. 由于没有人回答,我将给出解决方案。

My solution was to create a custom component and make callback to delegates of UITextView 我的解决方案是创建一个自定义组件并回调UITextView的委托

-(void)textViewDidChangeSelection:(UITextView *)textView
{  

  NSDictionary *event = @{
                          @"target": textView.reactTag,
                          @"highlights": @{
                              @"text": textView.text,
                              @"range": @(textView.selectedRange.length),
                              @"cursorPosition": @(textView.selectedRange.location),
                              @"eventType": @"textViewDidChangeSelection",
                              }
                          };
  [self.bridge.eventDispatcher sendInputEventWithName:@"topChange" body:event];
}

Then In my Javascript file 然后在我的Javascript文件中

setText(event: Event){
        if(event !== undefined){
        if(event.nativeEvent.highlights.eventType == "shouldChangeTextInRange" && event.nativeEvent.highlights.range > 0){
//do stuff
                }
            }   
        },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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