-目标:获取 UITextView 中特定文本部分的来源
以下是我正在开发的应用程序的屏幕截图的链接。请查看它,因为它更容易解释。图片链接:截图
这是填空式游戏的开始。我目前正在尝试获取每个下划线的 x,y 坐标。当点击屏幕底部的单词时,它将移动到下一个可用的下划线空间。
目前我已经编写了这段代码来做我需要的事情,但它非常难看,几乎不能工作并且不是很灵活。见下文:
// self.mainTextView is where the text with the underscores is coming from
NSString *text = self.mainTextView.text;
NSString *substring = [text substringToIndex:[text rangeOfString:@"__________"].location];
CGSize size = [substring sizeWithFont:self.mainTextView.font];
CGPoint p = CGPointMake((int)size.width % (int)self.mainTextView.frame.size.width, ((int)size.width / (int)self.mainTextView.frame.size.width) * size.height);
// What is going on here is for some reason everytime there is a new
// line my x coordinate is offset by what seems to be 10 pixels...
// So was my ugly fix for it..
// The UITextView width is 280
CGRect mainTextFrame = [self.mainTextView frame];
p.x = p.x + mainTextFrame.origin.x + 9;
if ((int)size.width > 280) {
NSLog(@"width: 280");
p.x = p.x + mainTextFrame.origin.x + 10;
}
if ((int)size.width > 560) {
NSLog(@"width: 560");
p.x = p.x + mainTextFrame.origin.x + 12;
}
if ((int)size.width > 840) {
p.x = p.x + mainTextFrame.origin.x + 14;
}
p.y = p.y + mainTextFrame.origin.y + 5;
// Sender is the button that was pressed
newFrame = [sender frame];
newFrame.origin = p;
[UIView animateWithDuration:0.2
delay:0
options:UIViewAnimationOptionAllowAnimatedContent|UIViewAnimationCurveEaseInOut
animations:^{
[sender setFrame:newFrame];
}
completion:^(BOOL finished){
}
];
所以对我来说最好的问题是 什么是解决这个问题的更好方法?或者你有什么建议吗?你会怎么做?
提前感谢您的时间。