我有一个应用程序,我使用CoreText
库来格式化一些文本。我使用以下设置样式设置:
- (void) setAttributes
{
CTTextAlignment alignment = self.alignment;
CGFloat spacing = self.spacing;
CTParagraphStyleSetting paragraphSettings[] =
{
{kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment), &alignment},
{kCTParagraphStyleSpecifierLineSpacing, sizeof(CGFloat), &spacing},
};
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(paragraphSettings, 2);
[self.text addAttribute:(NSString*)kCTParagraphStyleAttributeName value:(id)paragraphStyle range:NSMakeRange(0, [self.text.mutableString length])];
}
然后我有一个子类,它有一些不同的功能,需要添加更多的段落样式设置:
- (void) setAttributes
{
[super setAttributes];
CGFloat firstLineHeadIndent = 11, headIndent = 11, tailIndent = -11, paragraphSpacing = 0, paragraphSpacingBefore = 0;
CTParagraphStyleSetting paragraphSettings2[] =
{
{kCTParagraphStyleSpecifierFirstLineHeadIndent, sizeof(CGFloat), &firstLineHeadIndent},
{kCTParagraphStyleSpecifierHeadIndent, sizeof(CGFloat), &headIndent},
{kCTParagraphStyleSpecifierTailIndent, sizeof(CGFloat), &tailIndent},
{kCTParagraphStyleSpecifierParagraphSpacing, sizeof(CGFloat), ¶graphSpacing},
{kCTParagraphStyleSpecifierParagraphSpacingBefore, sizeof(CGFloat), ¶graphSpacingBefore}
};
CTParagraphStyleRef paragraphStyle2 = CTParagraphStyleCreate(paragraphSettings2, 5);
[self.text addAttribute:(NSString*)kCTParagraphStyleAttributeName value:(id)paragraphStyle2 range:NSMakeRange(0, [self.text.mutableString length])];
}
问题在于它不起作用,因为第二组CTParagraphStyleSetting's
覆盖了第一组。这样做的正确方法是什么,我尝试了很多事情,paragraphStyles
单独进行,并将属性单独添加到attributedString
,但无法正常工作。