【发布时间】:2016-03-10 05:20:24
【问题描述】:
我在根据客户要求设置文本对齐时遇到了一个小问题。客户希望文本以段落方式与单独行中的数字对齐。请看下图,该数字有 19 像素的内边距,文本以段落方式对齐,内边距为 41 像素。
如果我们将左对齐设置为标签,我们将得到数字下方的第二行。
我尝试搜索解决方案但无法成功。
提前致谢。
【问题讨论】:
标签: ios objective-c iphone uilabel
我在根据客户要求设置文本对齐时遇到了一个小问题。客户希望文本以段落方式与单独行中的数字对齐。请看下图,该数字有 19 像素的内边距,文本以段落方式对齐,内边距为 41 像素。
如果我们将左对齐设置为标签,我们将得到数字下方的第二行。
我尝试搜索解决方案但无法成功。
提前致谢。
【问题讨论】:
标签: ios objective-c iphone uilabel
您需要为问题部分和答案部分设置不同的属性。您已经这样做是为了控制字体,所以它不应该是一个大的变化。您需要为每个部分设置段落样式。对于问题部分,您需要将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;
【讨论】:
一种可能性:
另一种可能性:
【讨论】:
试试这个
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];
【讨论】: