[英]How to align UILabel text in paragraph
您需要為問題部分和答案部分設置不同的屬性。 你已經這樣做來控制字體,所以它不應該是一個很大的變化。 您需要為每個部分設置段落樣式。 對於問題部分,您需要將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];
一種可能性:
另一種可能性
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.