多行NSString,每行有一个字符限制

时间:2015-05-11 06:16:36

标签: ios objective-c iphone nsstring

我想构建一个多行NSString(用于打印目的),每行有一个字符限制。每行最多25个字符。行数可以是基于字符串长度的任何行。 这是我的方法,这不是我猜的最好的方法。什么是最好的方法,而无需手动检查字符串的长度?

NSString *strComment;
NSMutableString* strCustomerComments = [NSMutableString string];

if([strComment length]<=25){
   [strCustomerComments appendString:[NSString stringWithFormat:@"%@",strComment]];
}
else{
      [strCustomerComments appendString:[NSString stringWithFormat:@"%@\n%@",[[strComment substringToIndex:25],[strComment substringFromIndex:25]]];
   }  

3 个答案:

答案 0 :(得分:0)

我不知道它是否有用。以下是通过以下代码限制每行字符数的方法。

NSString* strTemp = @"Welcome to the jannat and here you will get idea about what happend what you need my support so let start to figure out issue.Welcome to the jannat and here you will get idea about what happend what you need my support so let start to figure out issue.Welcome to the jannat and here you will get idea about what happend what you need my support so let start to figure out issue.Welcome to the jannat and here you will get idea about what happend what you need my support so let start to figure out issue.Welcome to the jannat and here you will get idea about what happend what you need my support so let start to figure out issue.Welcome to the jannat and here you will get idea about what happend what you need my support so let start to figure out issue.Welcome to the jannat and here you will get idea about what happend what you need my support so let start to figure out issue.+++++";

    NSMutableString* strNewString = [[NSMutableString alloc] init];

    NSInteger addExtra = 0;

    NSInteger noofFixedCharecterPerLine = 25;


    for (int i=0; i<([strTemp length]/noofFixedCharecterPerLine); i++) {

        NSInteger FixedLength = noofFixedCharecterPerLine;
        if (i==(([strTemp length]/noofFixedCharecterPerLine))-1) {

            FixedLength = [strTemp length]-1 - ((i*noofFixedCharecterPerLine)+addExtra);


        }

        NSRange range = NSMakeRange((i*noofFixedCharecterPerLine)+addExtra, FixedLength);

        NSString* subString = [strTemp substringWithRange:range];

        if (i!=0) {
            [strNewString appendFormat:@"\n%@",subString];
            addExtra++;
        }
        else{
            [strNewString appendFormat:@"%@",subString];
        }
    }

    NSLog(@"print : %@",strNewString);

希望这会对你有所帮助。

答案 1 :(得分:0)

我有非常快速的方法-(NSArray *)breakStringIntoParts:(NSString *)sentence noOfParts:(int )noOfParts,这肯定有帮助。

以下是代码的输出: -

2015-05-11 13:04:25.120 vghnfgh[1011:15475] Line 1 Cntains 25 Characters i.e=I want to construct a mul
2015-05-11 13:04:25.121 vghnfgh[1011:15475] Line 2 Cntains 25 Characters i.e=ti line NSString (for pri
2015-05-11 13:04:25.122 vghnfgh[1011:15475] Line 3 Cntains 25 Characters i.e=nting purpose) with a cha
2015-05-11 13:04:25.122 vghnfgh[1011:15475] Line 4 Cntains 25 Characters i.e=racter limit per line. Ea
2015-05-11 13:04:25.122 vghnfgh[1011:15475] Line 5 Cntains 25 Characters i.e=ch line should have maxim
2015-05-11 13:04:25.122 vghnfgh[1011:15475] Line 6 Cntains 25 Characters i.e=um of 25 characters. Numb
2015-05-11 13:04:25.123 vghnfgh[1011:15475] Line 7 Cntains 25 Characters i.e=er of lines can be anythi
2015-05-11 13:04:25.123 vghnfgh[1011:15475] Line 8 Cntains 25 Characters i.e=ng based on the length of
2015-05-11 13:04:25.123 vghnfgh[1011:15475] Line 9 Cntains 25 Characters i.e= the string. This is my a
2015-05-11 13:04:25.123 vghnfgh[1011:15475] Line 10 Cntains 25 Characters i.e=pproach which is not the 
2015-05-11 13:04:25.123 vghnfgh[1011:15475] Line 11 Cntains 25 Characters i.e=best I guess. Whats is th
2015-05-11 13:04:25.139 vghnfgh[1011:15475] Line 12 Cntains 25 Characters i.e=e best way to do this wit
2015-05-11 13:04:25.139 vghnfgh[1011:15475] Line 13 Cntains 25 Characters i.e=hout manually checking th
2015-05-11 13:04:25.140 vghnfgh[1011:15475] Line 14 Cntains 25 Characters i.e=e length of the string?

此处的代码如下: -

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    line=@"I want to construct a multi line NSString (for printing purpose) with a character limit per line. Each line should have maximum of 25 characters. Number of lines can be anything based on the length of the string. This is my approach which is not the best I guess. Whats is the best way to do this without manually checking the length of the string?";

    NSMutableArray *arrSentenceParts=[self breakStringIntoParts:line noOfParts:25];

}

-(NSArray *)breakStringIntoParts:(NSString *)sentence noOfParts:(int )noOfParts
{
    NSMutableArray *arrResult=[[NSMutableArray alloc]init]; //to store character array each of 25 character long.

    NSMutableArray *range=[[NSMutableArray alloc]init];

    float rangeBreak =([sentence length] /noOfParts)+1; // this line divide how many objects will be there of 25 chars .

    for( int i=0;i<rangeBreak;i++)
    {
        [range addObject:[NSNumber numberWithInteger:i]]; // storing each range in an array;

    }

    NSInteger characterCount=0; // responsible for iterating the whole sentence
    for (int j=0; j < [range count]; j++) {  //looping and adding 25 characters in each range of arrResult
        NSMutableArray *characters = [[NSMutableArray alloc]init];
        for (NSInteger i=0; i < noOfParts; i++) {
            if(characterCount==[sentence length]) //stop the iteration on reaching last index of sentence
            {
                break;
            }
            NSString *ichar  = [NSString stringWithFormat:@"%c", [sentence characterAtIndex:characterCount]];
            [characters addObject:ichar]; //storing each  character in character array
            characterCount++;
        }
        [arrResult addObject:characters]; //now add final character array in arrResult
    }

    NSMutableArray *strMergedArr=[[NSMutableArray alloc]init]; //will be storing   Array of strings
    for(int k=0;k<[arrResult count];k++)
    {
        NSString *resultString = [[arrResult objectAtIndex:k] componentsJoinedByString:@""]; //merging all the characters in specific index of arrResult and storing in NSString

        [strMergedArr addObject:resultString]; // adding one by one in Merged Array of Strings
         NSLog(@"Line %d Cntains 25 Characters i.e=%@",k+1,[strMergedArr objectAtIndex:k]); // demo purpose can be commented

    }

    return strMergedArr; // finally returning the Array of Strings , and each string length 25 Charcters .


}

答案 2 :(得分:0)

好的,这是一个(未经测试):

- (NSString *)splitString:(NSString *)string byWidth:(NSUInteger)width
{
    NSUInteger index = 0, length = [string length];
    NSMutableString *outString = [NSMutableString new];
    while (length > 0) {
        if ([outString length] > 0)
            [outString appendString:@"\n"];
        NSUInteger lineLen = MIN(length - index, width);
        [outString appendString:[string substringWithRange:NSMakeRange(index, lineLen)]];
        length -= lineLen;
        index += lineLen;
    }
    return outString;
}