Objective-c每10个字符换行(保持单词)

时间:2012-02-28 21:51:04

标签: objective-c ios word-wrap line-breaks charactercount

如果我有一个字符串,我将加载到TextField中,如何进行换行符\ n每10个字符(包括空格和诸如此类)出现,但不会转到下一行中间单词... Like用最多10个字符包装文本?但是我不只是在UITextField中包装我实际上需要输入\ n因为它将被用于其他东西......(如果这很重要,这适用于iOS)任何帮助都表示赞赏我真的被卡住了! / p>

4 个答案:

答案 0 :(得分:5)

您可能希望在循环中使用NSScanner来组合这类内容,但要找出准确的算法,您需要准确说明您希望它如何工作。以下是您需要回答的问题:

  1. 为了包装文本,单词之间的多个空格的序列是否应“折叠”为单个空格,或者5个连续空格的序列是否应计为每行10个字符的5个字符?我假设您想要折叠多个空格而不是保留
  2. 如果您有一个大于10个字符的单词,您希望它如何工作?你希望它最终得到一个大于10个字符的行,或者你更喜欢插入换行符并在那种情况下强制中间行换行?我假设您希望允许超过10个字符的单词超出10个字符的限制。
  3. 考虑到这些关于你的问题的假设,我会这样编码:

    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];
    }
    

答案 1 :(得分: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--;
}

答案 2 :(得分: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,所以我写下了我的记忆(这并不总是完美无瑕)。因此代码中可能存在一些错误。

希望有所帮助

答案 3 :(得分: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);