1

When I use a .txt file in my app (even when I fix it up as much as I can) The lines at the end get truncated. I want the text to reach the end of the line (all the way to the right side of the screen, just like the first line) . Should I use another type of file? If so, which? and if not, how do I get the words to reach the end of the line?

Thanks

p.s. cleantxt doesnt fix the problem enter image description here

I want it to look like this

enter image description here

MendyK
  • 1,643
  • 1
  • 17
  • 30

2 Answers2

2

If I interpret your question correctly (via your example), this is not a question of truncation but a question of justification. You want "full justification" i.e. text stretching from left to right insets so the text appears as a rectangular block of text.

If yes and you are using a UITextView then:

For iOS 6 and earlier UITextView *textView = @""; // replace with property representing the text from your .txt file textView.textAlignment = NSTextAlignmentJustified;

For iOS 7+ (above is deprecated) You unfortunately need to either use CoreText or a UIWebView. See this post Justified Alignment in UITextView

Community
  • 1
  • 1
Wizkid
  • 1,015
  • 10
  • 10
0

Do you get the same result using this code?

NSString *fileName = @"impressum.txt";
NSURL *url = [[NSBundle mainBundle] URLForResource:[fileName stringByDeletingPathExtension] withExtension:[fileName pathExtension]];

NSError *error;
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithFileURL:url
                                                                               options:@{NSDocumentTypeDocumentAttribute:NSPlainTextDocumentType}
                                                                    documentAttributes:nil
                                                                                 error:&error];

CGRect paragraphRect = [attributedString boundingRectWithSize:CGSizeMake(_textLabel.frame.size.width), CGFLOAT_MAX)
                                                          options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                                          context:nil];

CGRect frame = _textLabel.frame;
frame.size.height = paragraphRect.size.height;
_textLabel.frame = frame;

[_textLabel setAttributedText:attributedString];
freshking
  • 1,824
  • 18
  • 31