How to count the number of lines in an Objective-C

2019-01-22 18:38发布

I want to count the lines in an NSString in Objective-C.

  NSInteger lineNum = 0;
  NSString *string = @"abcde\nfghijk\nlmnopq\nrstu";
  NSInteger length = [string length];
  NSRange range = NSMakeRange(0, length);
  while (range.location < length) {
      range = [string lineRangeForRange:NSMakeRange(range.location, 0)];
      range.location = NSMaxRange(range);
      lineNum += 1;
  }

Is there an easier way?

3条回答
啃猪蹄的小仙女
2楼-- · 2019-01-22 18:45

well, a not very efficient, but nice(ish) looking way is

NSString *string = @"abcde\nfghijk\nlmnopq\nrstu";
NSInteger length = [[string componentsSeparatedByCharactersInSet:
                                [NSCharacterSet newlineCharacterSet]] count];

Swift 4:

myString.components(separatedBy: .newlines)
查看更多
SAY GOODBYE
3楼-- · 2019-01-22 18:53

Apple recommends this method:

NSString *string;
unsigned numberOfLines, index, stringLength = [string length];

for (index = 0, numberOfLines = 0; index < stringLength; numberOfLines++)
    index = NSMaxRange([string lineRangeForRange:NSMakeRange(index, 0)]);

See the article. They also explain how to count lines of wrapped text.

查看更多
Summer. ? 凉城
4楼-- · 2019-01-22 18:55

If you want to take into account the width of the text, you can do this with TextKit (iOS7+):

func numberOfLinesForString(string: String, size: CGSize, font: UIFont) -> Int {
    let textStorage = NSTextStorage(string: string, attributes: [NSFontAttributeName: font])

    let textContainer = NSTextContainer(size: size)
    textContainer.lineBreakMode = .ByWordWrapping
    textContainer.maximumNumberOfLines = 0
    textContainer.lineFragmentPadding = 0

    let layoutManager = NSLayoutManager()
    layoutManager.textStorage = textStorage
    layoutManager.addTextContainer(textContainer)

    var numberOfLines = 0
    var index = 0
    var lineRange : NSRange = NSMakeRange(0, 0)
    for (; index < layoutManager.numberOfGlyphs; numberOfLines++) {
        layoutManager.lineFragmentRectForGlyphAtIndex(index, effectiveRange: &lineRange)
        index = NSMaxRange(lineRange)
    }

    return numberOfLines
}
查看更多
登录 后发表回答