How To justified the Text in UITextView?
I a开发者_如何转开发m having a UiTextView in That I want to Justified the text can anyone help me to do that
In ios 6 you can do justify text by using this code
UITextView *textView = //init your text view
textView.textAlignment = NSTextAlignmentJustified;
it doesnt look like its currently available with just the standard api's but here is a relatable stackoverflow solution.
How to change an UILabel/UIFont's letter spacing?
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];
}
精彩评论