2010-10-19 2 views
1

J'ai un UiTextView dans ce que je veux Justifier le texte peut quelqu'un m'aider à le faireComment justifier le texte dans UITextView?

+0

double possible de [Alignement Justifié dans UITextView - iPhone] (http://stackoverflow.com/questions/1301519/justified -alignment-in-uitextview-iphone) –

Répondre

0

Cela pourrait aider, je fait cela pour un UILabel, ce n'est pas parfait, je viens d'ajouter des espaces jusqu'à ce que chaque ligne remplit la largeur de rect de l'étiquette.

+ (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]; 
} 
4

En 6 ios vous pouvez faire justifier le texte en utilisant ce code

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