我有一个应用程序,在其中使用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,但是什么也做不了。

最佳答案

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

关于iphone - 在子类中附加更多CTParagraphStyleSetting,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11822436/

10-13 03:59