浅析CoreText自由绘制(4)-竖排绘制-如何绕中心点旋转-如何强行设置字体

转载请注明出处:http://blog.csdn.net/xcysuccess3/   

Coretext竖排绘制以及如何绕中心点旋转。以及如何设置字体。

   不废话。直接上代码。

//
//  CustomView.h
//  testWingdings
//
//  Created by 向晨宇 on 12-12-6.
//  Copyright (c) 2012年 向晨宇. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface CustomView : UIView
{
    NSString* ns_str;
}

@property(retain,nonatomic) NSString* ns_str;
@end





//  CustomView.m
//  testWingdings
//
//  Created by 向晨宇 on 12-12-6.
//  Copyright (c) 2012年 向晨宇. All rights reserved.
//

#import "CustomView.h"
#import <CoreText/CoreText.h>
#define ARCVIEW_DEFAULT_RADIUS 90.0

@implementation CustomView
@synthesize ns_str;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}
-(void)dealloc
{
    [ns_str release];
    [super dealloc];
}

-(NSDictionary*) getDicWithFont
{
    float kLabelFontSize = 44.0;
    CTFontRef ref = CTFontCreateWithName((CFStringRef)@"Arial", kLabelFontSize, NULL);
    NSMutableDictionary *attrDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:(id)ref, (NSString *)kCTFontAttributeName, nil];
    return attrDictionary;
}


- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    
    
    NSDictionary* attrDictionary = [self getDicWithFont];
    
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:ns_str attributes:attrDictionary];
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    
    CGContextSaveGState(context);
    CGAffineTransform textTransform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0,0.0, 0.0);
    CGContextSetTextMatrix(context,textTransform);
    CGContextTranslateCTM(context, 0, 44);
    
    CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)attString);        //9-4
    
    [attString release];
    CFArrayRef runArray = CTLineGetGlyphRuns(line);  //包装成一个数组
    
    CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runArray, 0);
    CFIndex runGlyphCount = CTRunGetGlyphCount(run);
    CTFontRef runFont = (CTFontRef)CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName);
    
    for (CFIndex runGlyphIndex = 0; runGlyphIndex < runGlyphCount; runGlyphIndex++) {
        
        CFRange glyphRange = CFRangeMake(runGlyphIndex, 1);
        CGGlyph glyph;
        CGPoint position;
        CTRunGetGlyphs(run, glyphRange, &glyph);
        CTRunGetPositions(run, glyphRange, &position);
        CGFontRef cgFont = CTFontCopyGraphicsFont(runFont, NULL);
        
        CGContextSetFont(context, cgFont);
        CGContextSetFontSize(context, CTFontGetSize(runFont));
        
        float ascent_temp  = 0.0f;
        float descent_temp = 0.0f;
        float leading_temp = 0.0f;
        
        NSNumber *widthValue = [NSNumber numberWithDouble:CTRunGetTypographicBounds((CTRunRef)run, CFRangeMake(runGlyphIndex, 1), &ascent_temp, &descent_temp, &leading_temp)];
        int width = [widthValue intValue];
        int height = ascent_temp+descent_temp+leading_temp;
        
        //  CGContextSetRGBFillColor(context, 0.9, 0.9, 0.1, 1.0);
        CGContextSaveGState(context);
        CGContextSetTextPosition(context, position.x+width/2, position.y + height/2);
     
        CGAffineTransform cgTransform2 =  CGAffineTransformMakeRotation(90*M_PI/180);
        CGAffineTransform result=CGAffineTransformConcat(textTransform, cgTransform2);
        
        CGContextSetTextMatrix(context, result);
        CGContextTranslateCTM(context,0, -width-(height-width)/2);
        
        CGContextShowGlyphsAtPoint(context, position.x , position.y, &glyph, 1);
        
        CGContextRestoreGState(context);
        CFRelease(cgFont);
    }
    CFRelease(line);
    CGContextRestoreGState(context);
    
}

-(UIFont*)customFont
{// 你的字体路径
    NSString *fontPath = [[NSBundle mainBundle] pathForResource:@"tom" ofType:@"ttf"];
    NSURL *url = [NSURL fileURLWithPath:fontPath];
    CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL(( CFURLRef)url);
    if (fontDataProvider == NULL)
        return nil;
    CGFontRef newFont = CGFontCreateWithDataProvider(fontDataProvider);
    CGDataProviderRelease(fontDataProvider);
    if (newFont == NULL) return nil;
    NSString *fontName = ( NSString *)CGFontCopyFullName(newFont);
    UIFont *font = [UIFont fontWithName:fontName size:12];
    CGFontRelease(newFont);
    return font;
}

@end





字体设置这一块折腾了很久,苹果的CTFontRef陷害了我。这个是强行设置的唯一方法。网上其他很多直接更改列表的都是不对的。那种不能兼容到所有的方法。字体绕中心点旋转也折腾了很久。本来用CONTEXT去平移,但是复杂情况还是不行。最后用矩阵解决。


版权所有:

http://blog.csdn.net/xcysuccess3/

向晨宇

QQ30513207

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值