0

我尝试了所有方法但没有用,因为我的逻辑能力很差......问题是我需要用uitextfield替换句子中的一个单词,让它看起来像一个空白,而且太动态了

例如:测试到达现场

我需要用 UItextfield 替换上面句子中的“Test”,以便用户可以输入 uitextfield。请告诉我更换它的方法..

我用 uitextview 来显示问题,代码是

UITextview   *textview = [[UITextView alloc] initWithFrame:CGRectMake(0.0, currentY, 320.0, myStringSize.height)];
textview.text = [NSString stringWithFormat:@"%i. %@", i+1, unhide];
textview.font = [UIFont fontWithName:@"Helvetica" size:16.0f];
CGRect frame = textview.frame;
frame.size.height = textview.contentSize.height;
textview.frame = CGRectMake(0.0, currentY, 320.0, frame.size.height+20);
textview.editable = NO;
[scrollMain addSubview:textview];

有什么办法可以吗...

或者有没有办法在textview中找出“Test”的坐标?帮帮我,提前谢谢。

4

3 回答 3

3

这会将“Test”一词替换为您选择的字符串:

textview.text = [textview.text stringByReplacingOccurrencesOfString:@"test" withString:@"SomeStringToReplaceWithTest"];
于 2013-01-28T11:14:11.773 回答
0

编辑:

在你的文本视图中选择“测试”,以编程方式找到它的范围

找到选定的文本范围:

UITextRange *selectionRange=[textView selectedTextRange];
CGRect selectionStartRect=[textView caretRectForPosition:selectionRange.start];
CGRect selectionEndRect=[textView caretRectForPosition:selectionRange.end];
CGPoint selectionCenterPoint=(CGPoint){(selectionStartRect.origin.x + selectionEndRect.origin.x)/2,(selectionStartRect.origin.y + selectionStartRect.size.height / 2)};

textField.frame现在用上面的初始化。

您的文本字段将出现在编写测试的地方。

于 2013-01-28T11:14:30.697 回答
0

执行以下步骤:

NSRange wordRange = NSMakeRange(0,10);
NSArray *firstWords = [[str componentsSeparatedByString:@" "] subarrayWithRange:wordRange];

您将数组元素中的所有单词然后通过上述方法之一替换您想要的任何世界。更新:

    NSString *test=@"Test arrives on the scene";
NSRange wordRange = NSMakeRange(0,10);
NSMutableArray *firstWords = (NSMutableArray *)[[test componentsSeparatedByString:@" "] subarrayWithRange:wordRange];
[firstWords replaceObjectAtIndex:indexOfstringyouWant withObject:yourTextField.text];

然后再次将字符串从数组中取出。

于 2013-01-28T11:18:35.643 回答