0

我有一个应用程序,我使用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), &paragraphSpacing},
        {kCTParagraphStyleSpecifierParagraphSpacingBefore, sizeof(CGFloat), &paragraphSpacingBefore}
    };

    CTParagraphStyleRef paragraphStyle2 = CTParagraphStyleCreate(paragraphSettings2, 5);
    [self.text addAttribute:(NSString*)kCTParagraphStyleAttributeName value:(id)paragraphStyle2 range:NSMakeRange(0, [self.text.mutableString length])];
}

问题在于它不起作用,因为第二组CTParagraphStyleSetting's覆盖了第一组。这样做的正确方法是什么,我尝试了很多事情,paragraphStyles单独进行,并将属性单独添加到attributedString,但无法正常工作。

4

1 回答 1

1

当您调用 super 时,您已经将段落样式设置设置为属性。再次获取相同的属性,用这些设置构造一个新的段落样式,根据需要用新值覆盖一些设置,最后将这个新的段落样式设置为一个属性。

于 2012-08-06T11:32:18.220 回答