Objective-c 每 23 个字符换行,不打断单词
Objective-c Line breaks every 23 characters without breaking words
我从这个 post Objective-c Line breaks every 10 characters (Keeping words in tact) 中找到了这个算法,问题是它从来没有 returns 最后一个词,它总是以某种方式丢弃它。
NSString *sourceStr = @"The Chocholate Experience in the Anna Ice Cream Museum - Spanish";
NSMutableString *resultString = [[NSMutableString alloc] init];
NSMutableString *currentLine = [[NSMutableString alloc] init];
NSMutableArray *stringsArray = [[NSMutableArray alloc] init];
NSScanner *scanner = [NSScanner scannerWithString:sourceStr];
NSString *scannedString = nil;
while ([scanner scanUpToCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString: &scannedString]) {
if ([currentLine length] + [scannedString length] <= 23) {
[currentLine appendFormat:@"%@ ", scannedString];
}
else if ([currentLine length] == 0) { // Newline but next word > 23
[resultString appendFormat:@"%@\n", scannedString];
}
else { // Need to break line and start new one
[resultString appendFormat:@"%@\n", currentLine];
[currentLine setString:[NSString stringWithFormat:@"%@ ", scannedString]];
}
[scanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:NULL];
}
NSLog(@"Result String: \n%@", resultString);
I get this as the output:
The Chocholate
Experience in the Anna
Ice Cream Museum -
去掉最后一个单词,即西班牙语。任何人都可以看到错误?我已经在这里待了几个小时了。任何帮助将不胜感激
如果您单步执行,您会注意到在您离开 while 循环之后,您遗漏的单词存储在 currentLine var 中。基本上,最后一个 else 条件将 "Ice Cream Museum -" 附加到 resultString,但是因为没有其他任何东西要扫描("Spanish" 已经保存在 scannedString 中——所以它已经被扫描),它只是放置当前行中的单词 "Spanish" 并且不会经过 while 循环的另一次迭代(因此永远不会将它放在要打印的 resultString 中)。
这是应该打印出最后一个词的修改后的代码:
NSString *sourceStr = @"The Chocholate Experience in the Anna Ice Cream Museum - Spanish";
NSMutableString *resultString = [[NSMutableString alloc] init];
NSMutableString *currentLine = [[NSMutableString alloc] init];
NSMutableArray *stringsArray = [[NSMutableArray alloc] init];
NSScanner *scanner = [NSScanner scannerWithString:sourceStr];
NSString *scannedString = nil;
while ([scanner scanUpToCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString: &scannedString]) {
// keep appending as long as we have something to append to
if ([currentLine length] + [scannedString length] <= 23) {
[currentLine appendFormat:@"%@ ", scannedString];
}
else if ([currentLine length] == 0) { // Newline but next word > 23
[resultString appendFormat:@"%@\n", scannedString];
}
else { // Need to break line and start new one
[resultString appendFormat:@"%@\n", currentLine];
[currentLine setString:[NSString stringWithFormat:@"%@ ", scannedString]];
}
[scanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:NULL];
}
// we may have something left over that couldn't fit into the last line
// that needs to be appended to the result string
if (currentLine.length > 0) {
[resultString appendFormat:@"%@\n", currentLine];
}
NSLog(@"Result String: \n%@", resultString);
我从这个 post Objective-c Line breaks every 10 characters (Keeping words in tact) 中找到了这个算法,问题是它从来没有 returns 最后一个词,它总是以某种方式丢弃它。
NSString *sourceStr = @"The Chocholate Experience in the Anna Ice Cream Museum - Spanish";
NSMutableString *resultString = [[NSMutableString alloc] init];
NSMutableString *currentLine = [[NSMutableString alloc] init];
NSMutableArray *stringsArray = [[NSMutableArray alloc] init];
NSScanner *scanner = [NSScanner scannerWithString:sourceStr];
NSString *scannedString = nil;
while ([scanner scanUpToCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString: &scannedString]) {
if ([currentLine length] + [scannedString length] <= 23) {
[currentLine appendFormat:@"%@ ", scannedString];
}
else if ([currentLine length] == 0) { // Newline but next word > 23
[resultString appendFormat:@"%@\n", scannedString];
}
else { // Need to break line and start new one
[resultString appendFormat:@"%@\n", currentLine];
[currentLine setString:[NSString stringWithFormat:@"%@ ", scannedString]];
}
[scanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:NULL];
}
NSLog(@"Result String: \n%@", resultString);
I get this as the output:
The Chocholate
Experience in the Anna
Ice Cream Museum -
去掉最后一个单词,即西班牙语。任何人都可以看到错误?我已经在这里待了几个小时了。任何帮助将不胜感激
如果您单步执行,您会注意到在您离开 while 循环之后,您遗漏的单词存储在 currentLine var 中。基本上,最后一个 else 条件将 "Ice Cream Museum -" 附加到 resultString,但是因为没有其他任何东西要扫描("Spanish" 已经保存在 scannedString 中——所以它已经被扫描),它只是放置当前行中的单词 "Spanish" 并且不会经过 while 循环的另一次迭代(因此永远不会将它放在要打印的 resultString 中)。
这是应该打印出最后一个词的修改后的代码:
NSString *sourceStr = @"The Chocholate Experience in the Anna Ice Cream Museum - Spanish";
NSMutableString *resultString = [[NSMutableString alloc] init];
NSMutableString *currentLine = [[NSMutableString alloc] init];
NSMutableArray *stringsArray = [[NSMutableArray alloc] init];
NSScanner *scanner = [NSScanner scannerWithString:sourceStr];
NSString *scannedString = nil;
while ([scanner scanUpToCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString: &scannedString]) {
// keep appending as long as we have something to append to
if ([currentLine length] + [scannedString length] <= 23) {
[currentLine appendFormat:@"%@ ", scannedString];
}
else if ([currentLine length] == 0) { // Newline but next word > 23
[resultString appendFormat:@"%@\n", scannedString];
}
else { // Need to break line and start new one
[resultString appendFormat:@"%@\n", currentLine];
[currentLine setString:[NSString stringWithFormat:@"%@ ", scannedString]];
}
[scanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:NULL];
}
// we may have something left over that couldn't fit into the last line
// that needs to be appended to the result string
if (currentLine.length > 0) {
[resultString appendFormat:@"%@\n", currentLine];
}
NSLog(@"Result String: \n%@", resultString);