I am having a UiTextView in That I want to Justified the text can anyone help me to do that
Asked
Active
Viewed 6,227 times
1
-
possible duplicate of [Justified Alignment in UITextView - iPhone](http://stackoverflow.com/questions/1301519/justified-alignment-in-uitextview-iphone) – August Lilleaas Oct 19 '10 at 12:49
3 Answers
4
In ios 6 you can do justify text by using this code
UITextView *textView = //init your text view
textView.textAlignment = NSTextAlignmentJustified;

Ali Raza
- 613
- 5
- 17
1
it doesnt look like its currently available with just the standard api's but here is a relatable stackoverflow solution.

Community
- 1
- 1

brian brinley
- 2,364
- 1
- 13
- 10
0
This might help, I made this for a UILabel, it's not perfect, I just add spaces until each line fills the width of the label's rect.
+ (void) justifyText:(UILabel*) label
{
NSString *text = label.text;
text = [text stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
UIFont *font = label.font;
CGRect rect = label.frame;
NSArray *wordArray = [text componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSMutableString *lineBreakText = [NSMutableString stringWithString:@""];
NSMutableArray *lines = [NSMutableArray array];
for (NSInteger i = 0; i < [wordArray count]; ++i)
{
NSMutableString *testStr = [NSMutableString stringWithString: lineBreakText];
if (i != 0)
{
[testStr appendString:@" "];
}
[testStr appendString:[wordArray objectAtIndex:i]];
CGSize testSize = [testStr sizeWithFont:font constrainedToSize:rect.size lineBreakMode:UILineBreakModeCharacterWrap];
if (testSize.height > [font lineHeight])
{
[lines addObject:lineBreakText];
lineBreakText = [NSMutableString stringWithString:@""];
[lineBreakText appendString:[wordArray objectAtIndex:i]];
}
else
{
if (i != 0)
{
[lineBreakText appendString:@" "];
}
[lineBreakText appendString:[wordArray objectAtIndex:i]];
}
if (i >= [wordArray count] - 1)
{
[lines addObject:lineBreakText];
}
}
NSMutableString *spacingText = [NSMutableString stringWithString:@""];
for (NSInteger i = 0; i < [lines count] - 1; ++i)
{
NSString *line = (NSString*)[lines objectAtIndex:i];
NSMutableString *spacedStr = [NSMutableString stringWithString:line];
NSArray *wordArray = [text componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSInteger spacesCount = [wordArray count] - 1;
if (spacesCount <= 0)
{
continue;
}
NSRange findInRng = NSMakeRange(0, [spacedStr length]);
NSRange spaceRng = [spacedStr rangeOfString:@" " options:0 range:findInRng];
CGSize testSize = [spacedStr sizeWithFont:font constrainedToSize:rect.size lineBreakMode:UILineBreakModeCharacterWrap];
while (testSize.height <= [font lineHeight])
{
if (spaceRng.location == NSNotFound)
{
findInRng = NSMakeRange(0, [spacedStr length]);
spaceRng = [spacedStr rangeOfString:@" " options:0 range:findInRng];
}
[spacedStr insertString:@" " atIndex:spaceRng.location];
testSize = [spacedStr sizeWithFont:font constrainedToSize:rect.size lineBreakMode:UILineBreakModeCharacterWrap];
findInRng = NSMakeRange(spaceRng.location + 2, [spacedStr length] - spaceRng.location - 2);
spaceRng = [spacedStr rangeOfString:@" " options:0 range:findInRng];
}
if (i != 0)
{
[spacingText appendString:@"\n"];
}
[spacingText appendString:spacedStr];
}
[spacingText appendString:@"\n"];
[spacingText appendString:(NSString*)[lines lastObject]];
[label setLineBreakMode:UILineBreakModeTailTruncation];
[label setText:spacingText];
}

richy
- 2,716
- 1
- 33
- 42