1

私は iOS で UITextView を使用していますが、次の入力テキストが別の新しい行を開始する瞬間/ポイントを取得したいと考えています。

どうすればこれを入手できますか?

-----編集済み-------

----長い間調査した結果、以下で問題ないことがわかりましたが、少し複雑なようです。

//limit text within text box
NSString *originalStr = textView.text;
textView.text = [textView.text stringByAppendingString:text];
CGFloat lineHeight = textView.font.lineHeight;
int currentLines = textView.contentSize.height / lineHeight;
int maxLines = textView.frame.size.height/lineHeight;
if (currentLines > maxLines) {
    NSString * firstHalfString = [originalStr substringToIndex:range.location];
    NSString * secondHalfString = [originalStr substringFromIndex: range.location];
    textView.text = [NSString stringWithFormat: @"%@%@%@",
                     firstHalfString,
                     text,
                     secondHalfString];
    int timeout = 0;
    while (true) {
        timeout =50;
        textView.text = [textView.text substringToIndex:(textView.text.length -1)];
        int newCurrentLines = textView.contentSize.height / lineHeight;
        if ((newCurrentLines == maxLines) || (timeout > 50))
            break;
    }

    textView.selectedRange = range;
    return false;
} else {
    textView.text = originalStr;
    textView.selectedRange = range;
    return true;
}
4

1 に答える 1

0

UITextView のこのデリゲート メソッドでは、このコードはテキスト フィールドが変更されるたびに行数をカウントします。

-(void)textViewDidChange:(UITextView *)textView
{
    UIFont *font = [UIFont boldSystemFontOfSize:11.0];
    CGSize size = [string sizeWithFont:font 
                     constrainedToSize:myUITextView.frame.size 
                         lineBreakMode:UILineBreakModeWordWrap]; // default mode
    float numberOfLines = size.height / font.lineHeight;
}
于 2013-08-26T12:52:28.900 に答える