gpt4 book ai didi

iphone - 如何在 iOS 中使用核心文本框架获取文本中的点击事件

转载 作者:行者123 更新时间:2023-11-29 11:13:56 24 4
gpt4 key购买 nike

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

我有这段代码让文本显示在 View 中

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 是包含所选章节圣经经文的字符串,因此当我单击第一节时,需要像下图一样选择它并弹出 subview 或警报 View ,如下图,通过选择必须将经文存储在某个地方或复制到剪贴板。multiPageView 是呈现核心文本的 View 。我想要就像在橄榄树圣经应用程序或 ibook 中一样。

由于 WebView 中的一些限制,我没有使用 WebView ..有什么想法可以做到这一点..,请帮助我。提前致谢。编辑:

- (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 文本。但我想选择这段文本。

最佳答案

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

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

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

如果您的 TextView 是可编辑的,您可以获得,

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

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

//Here is the complete sample code.

  {
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];
}

关于iphone - 如何在 iOS 中使用核心文本框架获取文本中的点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10293987/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com