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