1

我在 IPAD 中做一个阅读器应用程序,书籍数据存储在 Sqlite DB 中,我通过核心文本在视图中呈现 DB 的内容......我的文本格式是这样的,例如:1 haii 这是 iPhone。2 这是 iPad。3 这是麦克。等等等等..我只是想在触摸文本时获得触摸事件。例如,如果我点击第一句话,则必须选择任何颜色的第一句话,并触发弹出窗口(子视图或警报视图或操作表)像下面的图片在此处输入图像描述

我有这个代码来使文本显示在视图中

NSMutableString *combined = [NSMutableString string];

    for(NSUInteger idx = 0; idx < [delegatee.allSelectedVerseEnglish count]; idx++) {
        [combined appendFormat:@"  %d %@", 
         idx + 1, 
         [delegatee.allSelectedVerseEnglish objectAtIndex:idx]];

    }

    self.multiPageView.text =combined;

combined字符串是否包含所选章节的圣经经文,所以当我单击第一节经文时,需要像下图那样选择它并弹出一个子视图或警报视图,如下图所示,并且通过选择必须存储该诗句在某处或复制到剪贴板。multiPageView是呈现核心文本的视图。我想要就像在橄榄树圣经应用程序或 ibook 中一样。

我没有使用网络视图,由于网络视图的一些限制..有任何想法如何做到这一点..请帮助我。提前致谢。编辑:

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

    mainpopupview.frame =CGRectMake(0, 0, 768, 1004)    ;
    [self.view addSubview:mainpopupview];

    NSRange selectedRange = [textView selectedRange];
    NSString *backString = [maintextview.text substringToIndex:selectedRange.location];
    NSRange backRange = [backString rangeOfString:@"." options:NSBackwardsSearch];
    NSRange backRangee = [backString rangeOfString:@"." options:NSBackwardsSearch];
    int  myRangeLenght = backRangee.location - backRange.location;
    NSRange myStringRange = NSMakeRange (backRange.location, myRangeLenght);
    NSString *forwardString  = [maintextview.text substringFromIndex:backRange.location];
    NSLog(@"%@",[[forwardString componentsSeparatedByString:@"."] objectAtIndex:1]);
    NSLog (@"%@",  [maintextview.text substringWithRange:myStringRange]);

    NSString * myStringTxt = [[forwardString componentsSeparatedByString:@"."] objectAtIndex:1];
    NSLog(@"1 %@", myStringTxt);

      //  maintextview.textColor = [UIColor yellowColor];

    NSRange myStringRangee = [maintextview.text rangeOfString:myStringTxt];
    [maintextview select:self];
    maintextview.selectedRange = myStringRangee;
    }

myStringTxt 包含正确的诗句(.to 之间的menz 文本。但我想选择这个文本。

4

1 回答 1

2

我认为您可以将 UITextview 连接UIControlEventEditingChanged到某种方法。因此,当用户选择某些东西时,就会调用该方法。在该方法中,您可以使用以下方法获取所选文本,

NSRange range = [txtView selectedRange];
NSString *str = [txtView.text substringWithRange:range];

从选定的字符串中,您可以执行您喜欢的操作。

如果您的文本视图是可编辑的,您可以获得,

NSLog(@"%d",range.location);

知道位置后,您可以创建一个范围并选择字符串。有相关信息。

//这里是完整的示例代码。

  {
        sampleTextView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 60, 300)];
        sampleTextView.text = @"This is sample text to find text under touch. I am not going to use any private api.How am i supposed to do?.Let me see.";
        sampleTextView.editable = YES;
        sampleTextView.delegate = (id)self;
        [self.view addSubview:sampleTextView];
    }
- (void)textViewDidBeginEditing:(UITextView *)textView
{

    [NSTimer scheduledTimerWithTimeInterval:0.001 target:sampleTextView   selector:@selector(resignFirstResponder) userInfo:nil repeats:NO];
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    NSRange selectedRange = [textView selectedRange];
    NSString *backString = [sampleTextView.text substringToIndex:selectedRange.location];
    NSRange backRange = [backString rangeOfString:@" " options:NSBackwardsSearch];
    NSString *forwardString  = [sampleTextView.text substringFromIndex:backRange.location];
    NSLog(@"%@",[[forwardString componentsSeparatedByString:@" "] objectAtIndex:1]);
}

编辑 :

我已根据您的要求更新了代码。请在下面找到它,

UITextView *textview;
BOOL toggle;

- (void)viewDidLoad
{
    [super viewDidLoad];

    textview = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
    textview.delegate = (id)self;
    textview.inputView = [[UIView alloc] init];
    textview.text = @"Let's hope the text selection works programmatically!";
    [self.view addSubview:textview];
}

- (void)textViewDidChangeSelection:(UITextView *)textView
{    
    NSRange selectedRange = [textView selectedRange];
    NSString *backString = [textView.text substringToIndex:selectedRange.location];
    NSRange backRange = [backString rangeOfString:@" " options:NSBackwardsSearch];
    NSString *forwardString  = [textView.text substringFromIndex:backRange.location];

    NSLog(@"%@",[[forwardString componentsSeparatedByString:@" "] objectAtIndex:1]);

    NSRange ran = [textView.text rangeOfString:[[forwardString componentsSeparatedByString:@" "] objectAtIndex:1]];

    if (!toggle)
    {
        [self performSelector:@selector(selectText:) withObject:[NSValue valueWithRange:ran] afterDelay:0.001];
    }
    else
    {
        toggle = !toggle;
    }
}

-(void)selectText:(NSValue *)sender
{
    toggle = !toggle;

    textview.selectedRange = [sender rangeValue];
}
于 2012-04-24T08:13:18.633 回答