我有这段代码可以在 UITextview 中显示一些文本。我只想在这些字符串之间添加一个 UILabel。我正在以这种格式获取文本,1 这是第一个文本 2 这是第二个文本 3 这是第三个文本,我想要在数字字符和文本之间添加或附加 UILabel。我显示文本的代码是
NSMutableString *combined = [NSMutableString string];
for(NSUInteger idx = 0; idx < [delegatee.allSelectedVerseEnglish count]; idx++) {
[combined appendFormat:@" %d %@",
idx + 1,
[delegatee.allSelectedVerseEnglish objectAtIndex:idx]];
}
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
NSNumber * n = [f numberFromString:combined];
NSLog(@"N: %@", n);
maintextview.text =combined;
组合是上面格式中的文本,maintextview 是 UITextview。
我也得到了两个句子之间的字符串范围
- (void)textViewDidEndEditing:(UITextView *)textView
{
if (textView == maintextview) {
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;
有什么办法可以做到这一点。提前致谢。