Seperate long text to many line – by adding “\n”

– (void)getInformation {

NSString *text = self.textView.text;

// Remove \n, make normal text

NSArray *tokensWithoutNewLine = [text componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];

text = [tokensWithoutNewLine componentsJoinedByString:@” “];

// Split by white-space

NSArray *tokens = [text componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

//    NSLog(@”### oldText: %@”, self.textView.text);

NSLog(@”### tokens: %@”, tokens);

//    NSLog(@”### newText: %@”, text);

NSLog(@”\n”);

UIFont *font = self.textView.font;

NSLog(@”### pointSize: %f”, font.pointSize);

NSLog(@”### ascender: %f”, font.ascender);

NSLog(@”### descender: %f”, font.descender);

NSLog(@”### capHeight: %f”, font.capHeight);

NSLog(@”### xHeight: %f”, font.xHeight);

NSLog(@”### leading: %f”, font.leading);

NSLog(@”### *** lineHeight: %f”, font.lineHeight);

NSLog(@”\n”);

CGSize stringSize = [self.textView.text sizeWithFont:font];

NSLog(@”### stringSize: %@”, NSStringFromCGSize(stringSize));

//    CGFloat numberOfLine = stringSize.width / stringSize.height;

//    NSLog(@”### numberOfLine: %f”, numberOfLine);

//    NSLog(@”\n”);

//$pre_evaluated_line_count = ceil(sqrt($total_width / $height / $width_border * $height_border));

//$proper_width = $total_width / $pre_evaluated_line_count;

CGFloat preLineCountFloor = floor(sqrt(stringSize.width / stringSize.height));

CGFloat properWidthFloor = stringSize.width / preLineCountFloor;

NSLog(@”### (floor)preLineCount: %f”, preLineCountFloor);

NSLog(@”### (floor)properWidth: %f”, properWidthFloor);

NSLog(@”\n”);

//    CGFloat preLineCountCeil = ceil(sqrt(stringSize.width / stringSize.height));

//    CGFloat properWidthCeil = stringSize.width / preLineCountCeil;

//    NSLog(@”### (ceil)preLineCount: %f”, preLineCountCeil);

//    NSLog(@”### (ceil)properWidth: %f”, properWidthCeil);

//    NSLog(@”\n”);

NSMutableArray *arrayLines = [NSMutableArray new];

[arrayLines addObject:[NSMutableString new]];

NSInteger index = 0;

CGFloat multip = 1.3;

for (NSString *token in tokens) {

// Join token to current line

NSMutableString *newString = nil;

if ([arrayLines[index] length] > 0) {

newString = [NSMutableString stringWithFormat:@”%@ %@”, arrayLines[index], token];

}

else {

newString = [NSMutableString stringWithString:token];

}

// Detect if current width is higher or not

CGFloat currentWidth = [newString sizeWithFont:font].width;

NSLog(@”### Line: %@ — Width:%f — Proper:%f”, newString, currentWidth, properWidthFloor *multip);

if (currentWidth > properWidthFloor * multip) {

NSLog(@”### NewLine: %@”, token);

[arrayLines addObject:[[NSMutableString alloc] initWithString:token]];

index++;

continue;

}

else {

[arrayLines replaceObjectAtIndex:index withObject:newString];

NSLog(@”### CurrentLine: %@”, arrayLines[index]);

}

NSLog(@”\n”);

}

NSString *newString = [arrayLines componentsJoinedByString:@”\n”];

self.textView.text = newString;

NSLog(@”### newArrayLines: %@”, arrayLines);

NSLog(@”### newText:\nnna%@”, newString);

NSLog(@”\n”);

}