【问题标题】:How To justified the Text in UITextView?如何在 UITextView 中对齐文本?
【发布时间】:2010-10-19 12:45:25
【问题描述】:

我有一个 UiTextView 我想证明文本有没有人可以帮我做到这一点

【问题讨论】:

标签: iphone uitextview


【解决方案1】:

在 ios 6 中,您可以使用此代码来对齐文本

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

【讨论】:

    【解决方案2】:

    它看起来不像它目前仅适用于标准 api,但这里有一个相关的 stackoverflow 解决方案。

    How to change an UILabel/UIFont's letter spacing?

    【讨论】:

      【解决方案3】:

      这可能会有所帮助,我是为 UILabel 制作的,它并不完美,我只是添加空格,直到每行填满标签矩形的宽度。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-30
        • 2023-03-09
        • 1970-01-01
        • 2013-04-26
        • 1970-01-01
        • 2013-06-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多