I have an app where I use the CoreText
libraries to format some text. I use the following to set the style settings:
- (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])];
}
Then I have a subclass which has some different functionality that needs to add some more paragraphstyle settings:
- (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])];
}
The problem with this is it isn't working because the second set of CTParagraphStyleSetting's
is overwriting the first. What would be the correct way to go about doing this, I've tried numerous things, doing the paragraphStyles
individually, and adding the attributes individually to the attributedString
, but can't get anything to work.
When you call super, you will have set the paragraph style settings already as an attribute. Get the same attribute again, construct a new paragraph style with those settings, overriding some of the settings with new values as you'd like and finally set this new paragraph style as an attribute.