如果行号为2,则为UITextView

时间:2014-02-27 13:31:20

标签: ios objective-c uitextview

是否有检查UITextview中的行数?

我在互联网上搜索但我一无所获。

3 个答案:

答案 0 :(得分:3)

对于iOS7,这个版本要注意你可以在UITextView中使用不同的字体(和字体大小):

- (NSUInteger)numberOfLines
{
    NSLayoutManager *layoutManager = [textView layoutManager];
    NSUInteger index, numberOfLines;
    NSRange glyphRange = [layoutManager glyphRangeForTextContainer:[textView textContainer]];
    NSRange lineRange;

    for (numberOfLines = 0, index = glyphRange.location; index < glyphRange.length; numberOfLines++){
        (void) [layoutManager lineFragmentRectForGlyphAtIndex:index
                                               effectiveRange:&lineRange];
        index = NSMaxRange(lineRange);
    }
    return numberOfLines;
}

答案 1 :(得分:0)

用于计算文本行数

int numLines = textView.contentSize.height / textView.font.lineHeight;
NSLog(@"number of line - %d", numLines);

对于iOS 7 see this Question/Answer.

适用于iOS的EDIT&lt; 7:

这是正确答案

UIFont *myFont = [UIFont boldSystemFontOfSize:13.0]; // your font with size
CGSize size = [textView.text sizeWithFont:myFont  constrainedToSize:textView.frame.size  lineBreakMode:UILineBreakModeWordWrap]; // default mode
int numLines = size.height / myFont.lineHeight;
NSLog(@"number of line - %d", numLines);

答案 2 :(得分:0)

虽然你没有经常搜索,但这是一种方式:

CGSize size = [myString sizeWithFont:textView.font constrainedToSize:CGSizeMake(textView.frame.size.width,999) lineBreakMode:UILineBreakModeWordWrap];
int numLines = size.height / textView.font.lineHeight;