IOS:对齐 UI 标签中的文本在 UI 中不起作用
IOS: Justify text in UILabel not working in UI
我想在 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
和 trailing/leading:10
同样的问题,如果我在这里使用对齐来对齐文本
我不知道为什么这会发生在我身上。请给我一些修复它的说明。任何帮助将不胜感激
这应该有效。这是我从模拟器中得到的:
我做了什么:
- 将 UILabel 拖放到故事板上,并添加一些约束和颜色。
- 将文本设置为"attributed"
- 在文本字段中输入您的文本。
- 点击对齐。
- 换警察
- 行数 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
最后要做的是 运行 您的模拟器,看看它是否符合您的预期:
P.S:xCode 7.2 绝对有效。它适用于我的两个版本。
这是解决我现在问题的一种方法。我以编程方式为我的 UILabel
设置了 alignment justified
,它工作
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;
不要使用属性文本,只使用纯文本和 select 所有文本并使其对齐。我面临着同样的问题,通过在情节提要中更改归因于纯文本来修复它。
如果它不起作用,那么您必须通过代码修复它:-
使用属性文本:-
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;
希望对你有帮助...
这似乎是 UILabel 的一个错误,但您可以通过在故事板中进行微小的更改来修复它。
单击NSTextAlignments同一行的more按钮,添加一点Head Indent,例如0.1
。
您的 UILabel 可以正常工作。
如果它不能在情节提要或笔尖上运行,您可以通过编程方式完成
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;
我想在 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
和 trailing/leading:10
同样的问题,如果我在这里使用对齐来对齐文本
我不知道为什么这会发生在我身上。请给我一些修复它的说明。任何帮助将不胜感激
这应该有效。这是我从模拟器中得到的:
我做了什么:
- 将 UILabel 拖放到故事板上,并添加一些约束和颜色。
- 将文本设置为"attributed"
- 在文本字段中输入您的文本。
- 点击对齐。
- 换警察
- 行数 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
最后要做的是 运行 您的模拟器,看看它是否符合您的预期:
P.S:xCode 7.2 绝对有效。它适用于我的两个版本。
这是解决我现在问题的一种方法。我以编程方式为我的 UILabel
设置了 alignment justified
,它工作
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;
不要使用属性文本,只使用纯文本和 select 所有文本并使其对齐。我面临着同样的问题,通过在情节提要中更改归因于纯文本来修复它。 如果它不起作用,那么您必须通过代码修复它:-
使用属性文本:-
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;
希望对你有帮助...
这似乎是 UILabel 的一个错误,但您可以通过在故事板中进行微小的更改来修复它。
单击NSTextAlignments同一行的more按钮,添加一点Head Indent,例如0.1
。
您的 UILabel 可以正常工作。
如果它不能在情节提要或笔尖上运行,您可以通过编程方式完成
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;