ios - 获取 UITextView 中特定字符串每次出现的 Y 坐标以进行行编号

标签 ios objective-c uitextview

我试图获取 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/

相关文章:

ios - 以交互方式关闭键盘错误

iphone - 核心数据。 ExecuteFetchRequest 出现故障 NSManagedObject(不在 RAM 中)

iphone - 编辑嵌入的 UITextView 时 UITableView 滚动太远

ios - 如何在 nib/storyboard 的 UITextView 中换行?

iphone - 如何从pdf文件中的FileAttachment Annotation获取图像

ios - 在 UITextView 中获取 HTML

ios - 自动生成自定义初始化方法

iphone - 强制如何运作?

ios - 使用自动布局时何时将 imageView 添加到 UIButton

objective-c - 将 NSMutableDictionary 添加到 NSMutableArray 的特定索引处?