【问题标题】:How can I get the current selected line number inside UITextView?如何在 UITextView 中获取当前选定的行号?
【发布时间】:2016-09-20 08:52:12
【问题描述】:
I am using the below written method for getting the current selected line number of textView having attributed text. It works fine if last line is not blank. If last line goes blank this method is not giving a proper line number. 

Method : 

    - (NSInteger)getCurrentLineIndex{
        /*
         Get Current Line Inside UITextView
         */
        CGPoint cursorPosition = [self caretRectForPosition:self.selectedTextRange.start].origin;
        return (NSInteger)(cursorPosition.y / self.font.lineHeight);
    }

Is there any other way to get the current line index?


Fixed :  I already had mRangesArray in which each object gave a range of each line. Using those range objects and comparing with the selected range, I was able to fix the problem.

1) 获取范围数组 -(无效)分析 { if (self.mRangesArray != nil) { [self.mRangesArray removeAllObjects]; } NSString *string = self.text; NSUInteger numberOfLines, index, stringLength = [字符串长度];

    NSMutableArray *ranges = [NSMutableArray new];
    for (index = 0, numberOfLines = 0; index < stringLength; numberOfLines++)
    {
        NSRange range = [string lineRangeForRange:NSMakeRange(index, 0)];
        [ranges addObject:[NSValue valueWithRange:range]];
    }
    if (self.mRangesArray == nil) {
        self.mRangesArray = [[NSMutableArray alloc]init];
    }
    self.mRangesArray = ranges;
}

2) 比较 RangesArray 中的 Range 对象与当前选定的 Range
- (NSInteger)getCurrentLineIndex{ /* 此方法将用于获取 mRangesArray */ [自我分析];

    /*
     Get Current Line Inside UITextView
     */

    NSRange selectedRange = self.selectedRange;

   __block NSInteger index = [self.mRangesArray count]-1;
    [self.mRangesArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSRange objectRange = [obj rangeValue];
        if (*stop == NO) {
            if (NSLocationInRange(selectedRange.location, objectRange)) {
                index = idx;
                *stop =YES;
                return;
              }
        }
    }];

}

感谢大家的帮助。干杯:)

【问题讨论】:

    标签: ios uitextview uitextinput uitextposition


    【解决方案1】:

    编辑:我显然忽略了您问题的 current selected 部分。 我仍将代码保留在这里以防有人需要。

    你试过计算换行符吗?

    let str = "First\nSecond\nThird"
    let tok = str.componentsSeparatedByString("\n")
    print(tok.count) // Hopefully prints "3"
    

    【讨论】:

    • 这对于获取我正确获取的总行数是正确的。我需要的是获取行号,或者我们可以说是当前有光标的索引。
    • 是的,我明白了。只是过度阅读它。无论如何,你会遇到问题,因为你可能有一个可变的字体高度。如果没有正确的行分隔符,您将无法获得正确的行数。如果您能够正确获得总行数,则可以计算每行的字符数并将其除以光标位置。应该管用。虽然没试过,但我会的!
    • 我明白了。已用解决方案更新了上述问题。谢谢:)
    • 完美解决方案!我想我也要去试试!
    【解决方案2】:

    试试这段代码吧

    UIFont * fontNAme = [UIFont boldSystemFontOfSize:13.0]; 
    CGSize size = [textView.text fontNAme  constrainedToSize:textView.frame.size  lineBreakMode:UILineBreakModeWordWrap]; 
    int lineNumber = size.height / fontNAme .lineHeight;
    NSLog(@"line = %d", lineNumber);
    

    【讨论】:

    • 我不能使用常量字体,因为在这种情况下 textView 有richText 并且文本字体可以变化。
    猜你喜欢
    • 2013-07-07
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-06
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    相关资源
    最近更新 更多