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
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;
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.
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;
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
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];
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.