如何证明UITextView中的文本?

时间:2010-10-19 12:45:25

标签: iphone uitextview

我正在使用UiTextView我想要证明文本是否有人可以帮助我做到这一点

3 个答案:

答案 0 :(得分:4)

在ios 6中,您可以使用此代码

来对齐文本
 UITextView *textView = //init your text view 
textView.textAlignment = NSTextAlignmentJustified;

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

这可能会有所帮助,我为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];
}