Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何对齐段落中的UILabel文本_Ios_Objective C_Iphone_Uilabel - Fatal编程技术网

Ios 如何对齐段落中的UILabel文本

Ios 如何对齐段落中的UILabel文本,ios,objective-c,iphone,uilabel,Ios,Objective C,Iphone,Uilabel,我在按照客户要求设置文本对齐方面有一个小问题。客户希望文本以段落方式与单独行中的数字对齐。请参见下图,数字填充19像素,文本以段落方式对齐,填充41像素 如果我们将“左对齐”设置为“标签”,则会得到数字下方的第二行 我试图寻找解决办法,但没有成功 提前谢谢。试试这个 NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; paragraphStyle.a

我在按照客户要求设置文本对齐方面有一个小问题。客户希望文本以段落方式与单独行中的数字对齐。请参见下图,数字填充19像素,文本以段落方式对齐,填充41像素

如果我们将“左对齐”设置为“标签”,则会得到数字下方的第二行

我试图寻找解决办法,但没有成功

提前谢谢。

试试这个

NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.alignment = NSTextAlignmentLeft;
[self.titleString drawWithRect:CGRectMake(xOffset, yOffset, titleLabelSize.width, titleLabelSize.height)
                               options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine
                            attributes:@{ NSParagraphStyleAttributeName:paragraphStyle}
                               context:nil];
一种可能性:

  • 将NSAttributed字符串与自定义NSParagraphStyle一起使用。第一行缩进可以不同于其他行(左边距)
另一种可能性:

  • 使用web视图。您描述的内容很容易使用CSS进行配置

您需要为问题部分和答案部分设置不同的属性。你已经这样做来控制字体了,所以这不应该是一个很大的改变。您需要为每个部分设置段落样式。对于问题部分,需要将
firstLineHeadIndent
设置为19,将
headIndent
设置为41。对于答案部分,您需要将两者都设置为41

NSMutableParagraphStyle *questionStyle = [[NSMutableParagraphStyle alloc] init];
questionStyle.firstLineHeadIndent = 19;
questionStyle.headIndent = 41;
NSDictionary *questionAttributes = @{
    NSParagraphStyleAttributeName: questionStyle,
    NSFontAttributeName: [UIFont systemFontOfSize: 20]
};

NSMutableParagraphStyle *answerStyle = [questionStyle mutableCopy];
answerStyle.firstLineHeadIndent = questionStyle.headIndent;
NSDictionary *answerAttributes = @{
    NSParagraphStyleAttributeName: answerStyle,
    NSFontAttributeName: [UIFont systemFontOfSize: 16]
};

NSMutableAttributedString *richText = [[NSMutableAttributedString alloc] init];
[richText appendAttributedString:[[NSAttributedString alloc]
     initWithString:questionText attributes:questionAttributes]];
[richText appendAttributedString:[[NSAttributedString alloc]
    initWithString:answerText attributes:answerAttributes]];

label.attributedText = richText;

Hello kiran感谢您的回复,但我无法用上述代码完全满足我的要求。您能否编辑代码,并提供一些示例文本片段和屏幕截图,这将对我有帮助?Hello matt您能否首先为我添加示例代码片段?那对我完全有帮助。