两种方式 创建富文本的代码 ios +
2012-11-25 15:46
141 查看
#import <CoreText/CoreText.h>
#import <QuartzCore/QuartzCore.h>
NSAttributedString *getAttributedString(){
NSMutableAttributedString *attriString = [[[NSMutableAttributedString
alloc] initWithString:@"this is test!"]
autorelease];
//把this的字体颜色变为红色
[attriString addAttribute:(NSString *)kCTForegroundColorAttributeName
value:(id)[UIColor
redColor].CGColor
range:NSMakeRange(0,
4)];
//把is变为黄色
[attriString addAttribute:(NSString *)kCTForegroundColorAttributeName
value:(id)[UIColor
yellowColor].CGColor
range:NSMakeRange(5,
2)];
//改变this的字体,value必须是一个CTFontRef
[attriString addAttribute:(NSString *)kCTFontAttributeName
value:(id)CTFontCreateWithName((CFStringRef)[UIFont
boldSystemFontOfSize:14].fontName,
14,
NULL)
range:NSMakeRange(0,
4)];
//给this加上下划线,value可以在指定的枚举中选择
[attriString addAttribute:(NSString *)kCTUnderlineStyleAttributeName
value:(id)[NSNumber
numberWithInt:kCTUnderlineStyleDouble]
range:NSMakeRange(0,
4)];
return attriString;
}
@interface TView : UIView
@end
@implementation TView
- (id)initWithFrame:(CGRect)frame
{
self = [super
initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor
clearColor];
}
return self;
}
-(void)drawRect:(CGRect)rect{
[super drawRect:rect];
NSAttributedString *attriString =
getAttributedString();
CGContextRef ctx =
UIGraphicsGetCurrentContext();
CGContextConcatCTM(ctx,
CGAffineTransformScale(CGAffineTransformMakeTranslation(0, rect.size.height),
1.f, -1.f));
// CGContextTranslateCTM(ctx, 0, rect.size.height);
// CGContextScaleCTM(ctx, 1, -1);
CTFramesetterRef framesetter =
CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attriString);
CGMutablePathRef path =
CGPathCreateMutable();
CGPathAddRect(path, NULL, rect);
CTFrameRef frame =
CTFramesetterCreateFrame(framesetter, CFRangeMake(0,
0), path, NULL);
CFRelease(path);
CFRelease(framesetter);
CTFrameDraw(frame, ctx);
CFRelease(frame);
}
@end
@interface BIDViewController ()
@end
@implementation BIDViewController
- (void)viewDidLoad
{
[super
viewDidLoad];
TView *view = [[TView
alloc] initWithFrame:CGRectMake(0,
0, 200, 200)];
[self.view
addSubview:view];
[view release];
CATextLayer *textLayer = [CATextLayer
layer];
textLayer.string = getAttributedString();
textLayer.frame = CGRectMake(0,
CGRectGetMaxY(view.frame),
200, 200);
[self.view.layer
addSublayer:textLayer];
}
- (void)viewDidUnload
{
[super
viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation !=
UIInterfaceOrientationPortraitUpsideDown);
}
@end
转自 Created by zhangao on 5/30/12.
#import <QuartzCore/QuartzCore.h>
NSAttributedString *getAttributedString(){
NSMutableAttributedString *attriString = [[[NSMutableAttributedString
alloc] initWithString:@"this is test!"]
autorelease];
//把this的字体颜色变为红色
[attriString addAttribute:(NSString *)kCTForegroundColorAttributeName
value:(id)[UIColor
redColor].CGColor
range:NSMakeRange(0,
4)];
//把is变为黄色
[attriString addAttribute:(NSString *)kCTForegroundColorAttributeName
value:(id)[UIColor
yellowColor].CGColor
range:NSMakeRange(5,
2)];
//改变this的字体,value必须是一个CTFontRef
[attriString addAttribute:(NSString *)kCTFontAttributeName
value:(id)CTFontCreateWithName((CFStringRef)[UIFont
boldSystemFontOfSize:14].fontName,
14,
NULL)
range:NSMakeRange(0,
4)];
//给this加上下划线,value可以在指定的枚举中选择
[attriString addAttribute:(NSString *)kCTUnderlineStyleAttributeName
value:(id)[NSNumber
numberWithInt:kCTUnderlineStyleDouble]
range:NSMakeRange(0,
4)];
return attriString;
}
@interface TView : UIView
@end
@implementation TView
- (id)initWithFrame:(CGRect)frame
{
self = [super
initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor
clearColor];
}
return self;
}
-(void)drawRect:(CGRect)rect{
[super drawRect:rect];
NSAttributedString *attriString =
getAttributedString();
CGContextRef ctx =
UIGraphicsGetCurrentContext();
CGContextConcatCTM(ctx,
CGAffineTransformScale(CGAffineTransformMakeTranslation(0, rect.size.height),
1.f, -1.f));
// CGContextTranslateCTM(ctx, 0, rect.size.height);
// CGContextScaleCTM(ctx, 1, -1);
CTFramesetterRef framesetter =
CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attriString);
CGMutablePathRef path =
CGPathCreateMutable();
CGPathAddRect(path, NULL, rect);
CTFrameRef frame =
CTFramesetterCreateFrame(framesetter, CFRangeMake(0,
0), path, NULL);
CFRelease(path);
CFRelease(framesetter);
CTFrameDraw(frame, ctx);
CFRelease(frame);
}
@end
@interface BIDViewController ()
@end
@implementation BIDViewController
- (void)viewDidLoad
{
[super
viewDidLoad];
TView *view = [[TView
alloc] initWithFrame:CGRectMake(0,
0, 200, 200)];
[self.view
addSubview:view];
[view release];
CATextLayer *textLayer = [CATextLayer
layer];
textLayer.string = getAttributedString();
textLayer.frame = CGRectMake(0,
CGRectGetMaxY(view.frame),
200, 200);
[self.view.layer
addSublayer:textLayer];
}
- (void)viewDidUnload
{
[super
viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation !=
UIInterfaceOrientationPortraitUpsideDown);
}
@end
转自 Created by zhangao on 5/30/12.
相关文章推荐
- 黑马程序员--创建线程的两种方式、步骤以及代码体现
- 创建android逐帧动画的两种方式 布局和java代码
- java中synchronized修饰代码块(两种创建线程的方式讲解卖票程序)
- 两种方式(xib和代码)创建自定义collectionview
- 线程:创建线程有两种方式,一种是继承Thread类,另一种是实现Runnable接口。代码如下:
- Java多线程中线程的两种创建方式及比较代码示例
- spring+quartz两种整合方式:代码创建job+xml配置创建job
- 将Eclipse代码导入到Android Studio的两种方式
- 两种在linux下创建应用程序快捷方式的方法
- android启动第一个界面时即闪屏的核心代码(两种方式)
- 将Eclipse代码导入到AndroidStudio的两种方式
- css页面左中右分栏布局两种方式示例代码
- java多线程总结一:线程的两种创建方式及比较
- spring-01 spring的bean管理(使用配置文件方式创建对象 附代码)
- 创建线程的两种方式
- 创建线程的两种方式
- 使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用
- 用php或asp创建网页桌面快捷方式的代码
- 创建android逐帧动画的两种方式
- JAVA线程:两种不同创建方式全面解析