我试图获取 UITextView 中每个 \n
字符的 y 位置,以便我可以将行编号与 UITextView 对齐以获得准确的行号。我想做的是获取每次出现 \n
时的 Y 坐标数组。
目前,我可以获得当前单词的 CGSize,但我不太确定如何调整它以获得特定单词每次出现的 Y 坐标。
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSRange aRange = self.editorText.selectedRange;
if(range.location<self.editorText.text.length)
{
NSString * firstHalfString = [self.editorText.text substringToIndex:range.location];
NSString *temp = @"s";
CGSize s1 = [temp sizeWithFont:[UIFont systemFontOfSize:15]
constrainedToSize:CGSizeMake(self.view.bounds.size.width - 40, MAXFLOAT) // - 40 For cell padding
lineBreakMode:UILineBreakModeWordWrap]; // enter you textview font size
CGSize s = [firstHalfString sizeWithFont:[UIFont systemFontOfSize:15]
constrainedToSize:CGSizeMake(self.view.bounds.size.width - 40, MAXFLOAT) // - 40 For cell padding
lineBreakMode:UILineBreakModeWordWrap]; // enter you textview font size
//Here is the frame of your word in text view.
NSLog(@"xcoordinate=%f, ycoordinate =%f, width=%f,height=%f",s.width,s.height,s1.width,s1.height);
CGRect rectforword = CGRectMake(s.width, s.height, s1.width, s1.height);
// rectforword is rect of yourword
}
else
{
// Do what ever you want to do
}
return YES;
}
我不确定的主要部分是以我可以检索其坐标的方式获取字符的每次出现。
有办法做到这一点吗?或者有没有更简单的方法来实现行编号?
最佳答案
自定义解析您的 NSString 并将 #) 附加到每行的开头是一个选项吗?
这是一个非常重要的字符串解析和操作来完成,但它有一个额外的好处,即完全不必关心 yCoordinates 和其他基于布局的困惑参数。
关于ios - 获取 UITextView 中特定字符串每次出现的 Y 坐标以进行行编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19614148/