24

In my application, i have a UITextView with many lines, but I need the data with proper appearance.

Is it possible to have Justified Alignment in UITextView?

Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
sagarkothari
  • 24,520
  • 50
  • 165
  • 235

6 Answers6

71

I know it's an old question but now we have news about :

UITextView *textView = // init your text view
textView.textAlignment = NSTextAlignmentJustified;

And that's it !

UPDATE: This only works on iOS 6 and above

sam_smith
  • 6,023
  • 3
  • 43
  • 60
fencingCode
  • 1,427
  • 10
  • 13
  • do you have any idea why this only works on iOS6 and not iOS7? NSTextAlignmentJustified on my UILabel doesn't work in iOS7. – Rambatino Oct 12 '13 at 18:08
  • @MarkRamotowski, try using UITextView instead. works for me in iOS6 and iOS7 – Sam Nov 07 '13 at 11:59
  • @Sam I see that it does. However, I would prefer to keep it as a UILabel rather than a 'userInteraction = NO' UITextview.. – Rambatino Nov 07 '13 at 16:35
  • @MarkRamotowski did u find anything for ios7? – iosMentalist Aug 08 '14 at 09:33
  • @Shady Hi sorry for the late reply, I think it's a bug in UILabel that still exists i'm afraid. UITextView just seems to work better for things like this and custom fonts. – Rambatino Aug 11 '14 at 14:11
  • @MarkRamotowski Ya you r right.. i am using webview – iosMentalist Aug 11 '14 at 14:28
  • This no longer works in Xcode 7. However, the following link has a working solution: http://stackoverflow.com/questions/29829146/justify-aligning-text-in-a-text-view-in-swift – Wilson Apr 29 '16 at 01:21
12

I have come to a solution that works. First of all, you will need to change your UITextView and use a UIWebView instead.

Details.h

@interface Details : UIViewController {
    IBOutlet UIWebView *descripcion;
}

@property (nonatomic, retain) UIWebView *descripcion;

Then, load your UIWebView as follows:

Details.m

[descripcion loadHTMLString:[NSString stringWithFormat:@"<div align='justify'>%@<div>",YOUR_TEXT] baseURL:nil];
Graham Borland
  • 60,055
  • 21
  • 138
  • 179
Roger
  • 600
  • 5
  • 18
  • 1
    But then styling beyond the simple black text on white background needs to be done through CSS/inline html styles instead of cocoa properties. – autodidakto Jun 21 '11 at 20:42
10

There is solution:

NSString *text1 = @"Sample text : A his is my weblog in English, German and Korean. If you want to know more about a pizza stone once it’s cooled down.";

NSMutableParagraphStyle *style =  [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setAlignment: kCTJustifiedTextAlignment];

NSAttributedString * subText1 = [[NSAttributedString alloc] initWithString:text1 attributes:@{
                                                NSParagraphStyleAttributeName : style
                                     }];

_myUiTextView.attributedText = subText1;
Andro
  • 101
  • 1
  • 2
7

There is another option. CATextLayer.

It supports justified alignement explicitly. And it's a lot lightweight and faster than UIWebView. Because it's essentially a thin layer over CoreText, so does not require heavy layout process like UIWebView.

Or you can use one more another option, just using lower level CoreText framework directly.

Anyway, they doesn't support text editing. If you need it, you have to implement it all yourself.

Till
  • 27,559
  • 13
  • 88
  • 122
eonil
  • 83,476
  • 81
  • 317
  • 516
5

For Right To Left Languages like Persian

    UITextPosition *beginning = textview.beginningOfDocument;
    UITextPosition *start = [textview positionFromPosition:beginning offset:0];
    UITextPosition *end = [textview positionFromPosition:start offset:[textview.text length]];

    UITextRange *textRange = [textview textRangeFromPosition:start toPosition:end];
    [textview setBaseWritingDirection:UITextWritingDirectionRightToLeft forRange:textRange];

    textview.textAlignment = NSTextAlignmentJustified;
sagarkothari
  • 24,520
  • 50
  • 165
  • 235
Arash Zeinoddini
  • 801
  • 13
  • 19
3

There is no setting for justified alignment for text you only have center, left and right. If you want justified alignment where every line has the same amount of characters or something like that you can format the text entered to have carriage returns after x amount of characters or words, or something like that.

rptwsthi
  • 10,094
  • 10
  • 68
  • 109
Daniel
  • 22,363
  • 9
  • 64
  • 71
  • Sure, theres a page where you can suggest functionality on the apple site, i dont remember where it is tho =/ – Daniel Aug 19 '09 at 18:04