使用core text输出文本

在ios中,进行文本样式设计目前有2种方式: UIWebView ,core text
下面我将通过一个例子来讲述一下如何使用coretext来进行文本样式的添加。
本文章参考:
http://web.archiveorange.com/archive/v/nagQXJDPDGVNz9LFLmSK
http://www.cocoanetics.com/2011/01/befriending-core-text/
首先,进行创建一个UIView的子类,并实现如下代码:
- (void)drawRect:(CGRect)rect {
// Drawing code.
//创建要输出的字符串
NSString *longText = @”袁唯来来”;
//创建AttributeString
NSMutableAttributedString *string = [[NSMutableAttributedStringalloc]
initWithString:longText];
//创建字体以及字体大小
CTFontRef helvetica = CTFontCreateWithName(CFSTR(”Helvetica”),14.0, NULL);
CTFontRef helveticaBold =CTFontCreateWithName(CFSTR(”Helvetica-Bold”), 14.0, NULL);
//添加字体 目标字符串从下标0开始到字符串结尾
[string addAttribute:(id)kCTFontAttributeName
value:(id)helvetica
range:NSMakeRange(0, [string length])];
//添加字体 目标字符串从下标0开始,截止到4个单位的长度
[string addAttribute:(id)kCTFontAttributeName
value:(id)helveticaBold
range:NSMakeRange(0, 4)];
//添加字体 目标字符串从下标6开始,截止到5个单位长度
[string addAttribute:(id)kCTFontAttributeName
value:(id)helveticaBold
range:NSMakeRange(6, 5)];
//添加字体 目标字符串从下标109开始,截止到9个单位长度
[string addAttribute:(id)kCTFontAttributeName
value:(id)helveticaBold
range:NSMakeRange(109, 9)];
//添加字体 目标字符串从下标223开始,截止到6个单位长度
[string addAttribute:(id)kCTFontAttributeName
value:(id)helveticaBold
range:NSMakeRange(223, 6)];
//添加颜色,目标字符串从下标0开始,截止到4个单位长度
[string addAttribute:(id)kCTForegroundColorAttributeName
value:(id)[UIColor blueColor].CGColor
range:NSMakeRange(0, 4)];
//添加过程同上
[string addAttribute:(id)kCTForegroundColorAttributeName
value:(id)[UIColor redColor].CGColor
range:NSMakeRange(18, 3)];
[string addAttribute:(id)kCTForegroundColorAttributeName
value:(id)[UIColor greenColor].CGColor
range:NSMakeRange(657, 6)];
[string addAttribute:(id)kCTForegroundColorAttributeName
value:(id)[UIColor blueColor].CGColor
range:NSMakeRange(153, 6)];
//创建文本对齐方式
CTTextAlignment alignment = kCTLeftTextAlignment;//左对齐kCTRightTextAlignment为右对齐
CTParagraphStyleSetting alignmentStyle;
alignmentStyle.spec=kCTParagraphStyleSpecifierAlignment;//指定为对齐属性
alignmentStyle.valueSize=sizeof(alignment);
alignmentStyle.value=&alignment;
//创建文本行间距
CGFloat lineSpace=5.0f;//间距数据
CTParagraphStyleSetting lineSpaceStyle;
lineSpaceStyle.spec=kCTParagraphStyleSpecifierLineSpacing;//指定为行间距属性
lineSpaceStyle.valueSize=sizeof(lineSpace);
lineSpaceStyle.value=&lineSpace;
//创建样式数组
CTParagraphStyleSetting settings[]={
alignmentStyle,lineSpaceStyle
};
//设置样式
CTParagraphStyleRef paragraphStyle =CTParagraphStyleCreate(settings, sizeof(settings));
//给字符串添加样式attribute
[string addAttribute:(id)kCTParagraphStyleAttributeName
value:(id)paragraphStyle
range:NSMakeRange(0, [string length])];
// layout master
CTFramesetterRef framesetter =CTFramesetterCreateWithAttributedString(
(CFAttributedStringRef)string);
CGMutablePathRef leftColumnPath = CGPathCreateMutable();
CGPathAddRect(leftColumnPath, NULL,
CGRectMake(0, 0,
self.bounds.size.width,
self.bounds.size.height));
CTFrameRef leftFrame =CTFramesetterCreateFrame(framesetter,
CFRangeMake(0, 0),
leftColumnPath, NULL);
// flip the coordinate system
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// draw
CTFrameDraw(leftFrame, context);
// cleanup
CGPathRelease(leftColumnPath);
CFRelease(framesetter);
CFRelease(helvetica);
CFRelease(helveticaBold);
[string release];
UIGraphicsPushContext(context);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值