0

我正在尝试创建一个UILabel其中一些文本向右对齐而一些文本向左对齐的位置。它类似于UITableViewCell带有小箭头的 :

在此处输入图像描述

我正在尝试使用NSAttributedString,但无法弄清楚解决此问题的正确方法是什么。

这是一些不起作用的代码。它向右对齐。

NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:@"Label >"];

        NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
        paragraph.alignment = NSTextAlignmentLeft;

        [att addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, @"Label".length)];

        NSMutableParagraphStyle *rightParagraph = [[NSMutableParagraphStyle alloc] init];
        paragraph.alignment = NSTextAlignmentRight;
        [att addAttribute:NSParagraphStyleAttributeName value:rightParagraph range:NSMakeRange(5, 1)];
4

3 回答 3

0

您可以使用 NSAttributedString 来满足您的要求,但使用两个 UILabel 会更好、更简洁。

于 2015-04-06T12:52:33.233 回答
0

Use 2 labels.Assign the needed TextAlignment property to them. And after setting label text value, write this line :

[textLabel sizeToFit];

Though sizes of the labels varies it will set to minimum size. and will avoid text overlap.

于 2015-04-06T14:14:20.670 回答
0

我以前用那个代码做过,希望它也对你有用。

NSString* alphaString = @“some text”;

NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentLeft;

NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] 
                                               initWithString:alphaString 
                                               attributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                               [UIFont fontWithName:@"HelveticaNeue" size:13],      NSFontAttributeName, 
    paragraphStyle, NSParagraphStyleAttributeName, nil]];

NSString * betaString = @“some other text”;

NSMutableParagraphStyle* paragraphStyle2 = [[NSMutableParagraphStyle alloc] init];
paragraphStyle2.alignment = NSTextAlignmentRight;


[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:betaString attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"HelveticaNeue" size:13], NSFontAttributeName, paragraphStyle2, NSParagraphStyleAttributeName, nil]]];

yourLabel.attributedText = attributedString;
于 2015-04-06T13:15:07.750 回答