如果我有一个要加载到 TextField 的字符串,我怎样才能使换行符 \n 每 10 个字符(包括空格和诸如此类)发生一次,但不能转到下一行中间字...就像换行最多10个字符?但我不只是包装在 UITextField 中,我实际上需要输入 \n,因为它也将用于其他东西......(如果这很重要,这适用于 iOS)任何帮助,我真的被卡住了!
问问题
2871 次
4 回答
5
您可能想在一个循环中使用 NSScanner 来组合这种东西,但是要找出精确的算法,您需要明确说明您希望它如何工作。以下是您需要回答的问题:
- 是否应该将单词之间的多个空格序列“折叠”成一个空格以换行以换行,或者应该将 5 个连续空格的序列计为 5 个字符,而不是每行 10 个字符?我假设您希望折叠多个空间而不是保留
- 如果您有一个大于 10 个字符的单词,您希望它如何工作?您是否希望它以大于 10 个字符的行结束,或者您希望在这种情况下插入换行符并强制中间换行?我假设您希望允许超过 10 个字符的单词扩展到超过 10 个字符的限制。
考虑到有关您的问题的这些假设,我将对其进行编码:
NSMutableString *resultString = [[NSMutableString alloc] init];
NSMutableString *currentLine = [[NSMutableString alloc] init];
NSScanner *scanner = [NSScanner scannerWithString:sourceString];
NSString *scannedString = nil;
while ([scanner scanUpToCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString: &scannedString]) {
if ([currentLine length] + [scannedString length] <= 10) {
[currentLine appendFormat:@"%@ ", scannedString];
}
else if ([currentLine length] == 0) { // Newline but next word > 10
[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];
}
于 2012-02-28T23:05:00.527 回答
2
我创建了一些实际中断的东西,不是每 10 个字符,而是在单词之间,不是没有断字引擎,这需要字典。我根据 Perceptions 的答案构建了它。
NSMutableString * guid = [NSMutableString stringWithString: @"Some suitably long string will be used for demonstration purposes"];
int lastSpace=0;
int lastbreak=0;
NSUInteger i = [guid length];
while(i > 0) {
if ([guid characterAtIndex:i-1]==' ')
lastSpace=i;
lastbreak++;
if (lastbreak>9) {
if (lastSpace!=0) {
[guid insertString:@"\n" atIndex: lastSpace];
} else {
// we have not found a space in 10 chars, so break where there is no space.
// no H&J engine here, so we can add the - or not.
[guid insertString:@"-\n" atIndex: i];
i++; // since were adding a character, dont skip a character.
}
lastbreak=0;
lastSpace=0;
}
i--;
}
于 2012-02-28T22:44:16.940 回答
1
我会这样做:
NSString *finalString = @"";
NSString *yourString = @"This is your very very long string with many words and letters and spaces, haha";
NSArray *someArray = [yourString componentsSeparatedByString:@" "]; //that will make an array with strings separated by spaces
for (int i = 0; i < [someArray count]; i++) {
NSArray *someArray2 = [finalString componentsSeparatedByString:@"\n"];
if ([[someArray objectAtIndex:i]length] + [[someArray2 lastObject]length] > 9 )
finalString = [NSString stringWithFormat:@"%@\n%@", finalString, [someArray objectAtIndex:i]];
else
finalString = [NSString stringWithFormat:@"%@ %@", finalString, [someArray objectAtIndex:i]];
}
我相信这样的事情应该有效。我附近没有 Mac,所以我把它记下来了(这并不总是完美无缺的)。因此,代码中可能存在一些错误。
希望能帮助到你
于 2012-02-28T22:43:27.733 回答
0
您必须自己在字符串中插入换行符,这个 afaik 没有内置函数。
NSMutableString * guid = [NSMutableString stringWithString: @"Some suitably long string will be used for demonstration purposes"];
for(NSUInteger i = 10; i < [guid length] ; i += 10) {
[guid insertString:@"\n" atIndex: i];
}
NSLog(@"GUID: %@", guid);
于 2012-02-28T22:26:56.177 回答