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?
NSMutableString* strCustomerComments = [NSMutableString string];
if([[[arrItems objectAtIndex:0]objectForKey:@"CustomerComment"] length]<=25){
[strCustomerComments appendString:[NSString stringWithFormat:@"%@",[[arrItems objectAtIndex:0]objectForKey:@"CustomerComment"]]];
}
else{
[strCustomerComments appendString:[NSString stringWithFormat:@"%@\n%@",[[[arrItems objectAtIndex:0]objectForKey:@"CustomerComment"] substringToIndex:25],[[[arrItems objectAtIndex:0]objectForKey:@"CustomerComment"] substringFromIndex:25]]];
}
Aucun commentaire:
Enregistrer un commentaire