问题
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?
回答1:
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)
回答2:
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.
回答3:
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
}
来源:https://stackoverflow.com/questions/1085524/how-to-count-the-number-of-lines-in-an-objective-c-string-nsstring