9

我想对齐我的文本,UILabel但它似乎不起作用。然而,另一个对齐是左、右、中心工作。
我正在使用 XCode 7.2。我已经在模拟器和真实设备上进行了测试,但它产生了同样的问题

对齐对齐 在此处输入图像描述

我的文字:

Don't worry, your data will not be sold.Don't worry,your data wills not be sold. Connecting your accounts will benefit your E score and your profile viewing experience. Don't worry, your data will not be sold.Don't worry, your data wills not be sold. Connecting your accounts will benefit your ECT score and your profile viewing experience.

带字体:Helvetica Neue 13.0和尾随/前导:10

如果我在这里使用 align 来证明文本是同样的问题 在此处输入图像描述

我不知道为什么这会发生在我身上。请给我一些修复它的指导。任何帮助将不胜感激

4

5 回答 5

5

这似乎是 UILabel 的一个错误,但您可以通过在情节提要中进行微小更改来修复它。
点击NSTextAlignments 同一行的more按钮,添加一点 Head Indent ,比如0.1 .
在此处输入图像描述

你的 UILabel 可以正常工作。
在此处输入图像描述

于 2016-04-03T10:08:01.400 回答
2

这应该有效。这是我从模拟器中得到的: 在此处输入图像描述

我做了什么:

  1. 在情节提要上拖放一个 UILabel 并添加一些约束和颜色。
  2. 将文本设置为“归属”
  3. 将您的文本放在文本字段中。
  4. 点击证明。
  5. 换警察
  6. 行数 0。

此时你应该有这个: 在此处输入图像描述

现在从情节提要到您的控制器添加一个 IBOutlet(Ctrl + 将其拖动到控制器的顶部)。它应该是这样的: 在此处输入图像描述

现在在 viewDidLoad fct 中添加一些代码:

let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = NSTextAlignment.Justified

        let attributedString = NSAttributedString(string: label.text!, attributes: [ NSParagraphStyleAttributeName: paragraphStyle, NSBaselineOffsetAttributeName: NSNumber(float: 0)
            ])

        label.attributedText = attributedString
        label.numberOfLines = 0

最后要做的是运行你的模拟器,看看它是否符合你的预期: 在此处输入图像描述

PS:使用 xCode 7.2 绝对有效。它在两个版本上都适用于我。

于 2016-03-25T10:47:00.363 回答
1

这是解决我现在问题的一种解决方案。alignment justified我以编程方式为我设置了UILabel它并且它工作

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

    NSDictionary *attribute = @{
                                        NSParagraphStyleAttributeName: paragraph,
                                        NSFontAttributeName: self.describesLabel.font,
                                        NSBaselineOffsetAttributeName:[NSNumber numberWithFloat:0]
                                        };
    NSAttributedString *attributeMessage = [[NSAttributedString alloc] initWithString:self.describesLabel.text attributes:attribute];
    self.describesLabel.attributedText = attributeMessage;
于 2016-03-28T10:15:10.113 回答
0

不要使用属性文本,只需使用纯文本并选择所有文本并使其合理。我面临着同样的问题,通过在 Storyboard 中更改归因于纯文本来解决它。如果它不起作用,那么您必须通过代码修复它:-

使用属性文本:-

 NSMutableParagraphStyle *paragraphStyle = NSMutableParagraphStyle.new;
 paragraphStyle.alignment                = NSTextAlignmentJustified;

 NSDictionary *attrsDictionary           = [NSDictionary dictionaryWithObjectsAndKeys:yourFont,NSFontAttributeName,paragraphStyle,NSParagraphStyleAttributeName, nil];

 NSAttributedString *attributeMessage    = [[NSAttributedString alloc] initWithString:yourTextString attributes:attrsDictionary];
 self.yourLabel.attributedText           = attributeMessage;

使用纯文本:-

self.yourLabel.textAlignment             = NSTextAlignmentJustified;
self.yourLabel.font                      = yourFont;
self.yourLabel.text                      = yourTextString;

希望能帮助到你...

于 2016-04-02T12:51:57.607 回答
0

如果它不能从情节提要或笔尖工作,您可以以编程方式进行

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentJustified;


NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"Your text here"];
                                                             attributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                         paragraphStyle, NSParagraphStyleAttributeName ,
                                                                         [NSNumber numberWithFloat:0],NSBaselineOffsetAttributeName,
                                                                         nil]];

self.yourLabel.attributedText = string;
于 2016-04-03T17:52:41.333 回答