NSAttributedString插入一个项目符号点? [英] NSAttributedString inserting a bullet point?

查看:89
本文介绍了NSAttributedString插入一个项目符号点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个NSAttributedString,我想在一部分文本的开头插入一个bullet point.我怎样才能做到这一点?显示文字时,如何创建CTPAragraphStyle来创建此项目符号点?

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?

修改: 应该可以在 iOS

推荐答案

最简单的方法:[mutableAttributedString insertAttributedString:@•\ t" atIndex:0];

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), &paragraphSpacing},
    { kCTParagraphStyleSpecifierParagraphSpacingBefore, sizeof(CGFloat), &paragraphSpacingBefore}
}; 

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);

您需要包括CoreText框架,然后导入CoreText/CoreText.h

You need to include the CoreText framework and then import CoreText/CoreText.h

这篇关于NSAttributedString插入一个项目符号点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆