So I have an NSAttributedString
I want to insert a bullet point
at the beginning of a portion of text. How can I do this? How do I create a CTPAragraphStyle
that creates this bullet point when I display the text?
Edit: Should be available on iOS
The easy bit: [mutableAttributedString insertAttributedString: @"•\t" atIndex:0];
The hard bit. Something along the following lines. (This is extracted from a bigger project, but it may give you a decent start.)
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"•\texample bullet fill out the text to check what happens on the second line and make sure it is lining up OK"];
CTTextAlignment alignment = kCTLeftTextAlignment;
CGFloat paragraphSpacing = 0.0;
CGFloat paragraphSpacingBefore = 0.0;
CGFloat firstLineHeadIndent = 15.0;
CGFloat headIndent = 30.0;
CGFloat firstTabStop = 15.0; // width of your indent
CGFloat lineSpacing = 0.45;
CTTextTabRef tabArray[] = { CTTextTabCreate(0, firstTabStop, NULL) };
CFArrayRef tabStops = CFArrayCreate( kCFAllocatorDefault, (const void**) tabArray, 1, &kCFTypeArrayCallBacks );
CFRelease(tabArray[0]);
CTParagraphStyleSetting altSettings[] =
{
{ kCTParagraphStyleSpecifierLineSpacing, sizeof(CGFloat), &lineSpacing},
{ kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment), &alignment},
{ kCTParagraphStyleSpecifierFirstLineHeadIndent, sizeof(CGFloat), &firstLineHeadIndent},
{ kCTParagraphStyleSpecifierHeadIndent, sizeof(CGFloat), &headIndent},
{ kCTParagraphStyleSpecifierTabStops, sizeof(CFArrayRef), &tabStops},
{ kCTParagraphStyleSpecifierParagraphSpacing, sizeof(CGFloat), ¶graphSpacing},
{ kCTParagraphStyleSpecifierParagraphSpacingBefore, sizeof(CGFloat), ¶graphSpacingBefore}
};
CTParagraphStyleRef style;
style = CTParagraphStyleCreate( altSettings, sizeof(altSettings) / sizeof(CTParagraphStyleSetting) );
if ( style == NULL )
{
NSLog(@"*** Unable To Create CTParagraphStyle in apply paragraph formatting" );
return;
}
[string addAttributes:[NSDictionary dictionaryWithObjectsAndKeys:(NSObject*)style,(NSString*) kCTParagraphStyleAttributeName, nil] range:NSMakeRange(0,[string length])];
CFRelease(tabStops);
CFRelease(style);
You need to include the CoreText framework and then import CoreText/CoreText.h