Justified Alignment in UITextView - iPhone
Asked Answered
D

6

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?

Delanie answered 19/8, 2009 at 17:44 Comment(0)
R
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

Representationalism answered 2/11, 2012 at 22:21 Comment(7)
do you have any idea why this only works on iOS6 and not iOS7? NSTextAlignmentJustified on my UILabel doesn't work in iOS7.Cyrille
@MarkRamotowski, try using UITextView instead. works for me in iOS6 and iOS7Ericson
@Ericson I see that it does. However, I would prefer to keep it as a UILabel rather than a 'userInteraction = NO' UITextview..Cyrille
@MarkRamotowski did u find anything for ios7?Efrainefram
@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.Cyrille
@MarkRamotowski Ya you r right.. i am using webviewEfrainefram
This no longer works in Xcode 7. However, the following link has a working solution: #29829646Adopted
R
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];
Reynaud answered 1/4, 2011 at 14:45 Comment(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.Barley
O
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;
Orthodoxy answered 26/11, 2012 at 14:8 Comment(0)
S
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.

Sixtyfour answered 24/4, 2011 at 6:4 Comment(0)
I
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;
Inaccurate answered 21/7, 2014 at 6:40 Comment(0)
W
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.

Wenger answered 19/8, 2009 at 17:55 Comment(1)
Sure, theres a page where you can suggest functionality on the apple site, i dont remember where it is tho =/Wenger

© 2022 - 2024 — McMap. All rights reserved.