转载请注明出处: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
版权所有:
http://blog.csdn.net/xcysuccess3/
向晨宇
QQ30513207