| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- //
- // AttributedLabel.m
- // AttributedStringTest
- //
- // Created by sun huayu on 13-2-19.
- // Copyright (c) 2013年 sun huayu. All rights reserved.
- //
- #import "AttributedLabel.h"
- #import <QuartzCore/QuartzCore.h>
- @interface AttributedLabel(){
- UIImageView *imageView;
- CGRect strikeRect;
- }
- @end
- @implementation AttributedLabel
- @synthesize attString = _attString;
- - (void)dealloc
- {
- [_attString release];
- [super dealloc];
- }
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- // Initialization code
- }
- return self;
- }
- - (id)initWithFrame:(CGRect)frame strikethroughPosition:(CGRect) _strikeRect
- {
-
- self = [super initWithFrame:frame];
- if (self) {
- strikeRect = _strikeRect;
- }
- return self;
- }
- - (void)drawRect:(CGRect)rect{
-
- CATextLayer *textLayer = [CATextLayer layer];
- textLayer.string = _attString;
- textLayer.transform = CATransform3DMakeScale(0.5,0.5,1);
- textLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
- CGRect lineRect = CGRectMake(strikeRect.origin.x, strikeRect.origin.y, strikeRect.size.width, 1.0);
- CGContextRef context = UIGraphicsGetCurrentContext();
- // CGContextSetStrokeColorWithColor(context, [UIColor lightGrayColor].CGColor);
- CGContextFillRect(context, lineRect);
- [self.layer addSublayer:textLayer];
- }
- - (void)setText:(NSString *)text{
- [super setText:text];
- if (text == nil) {
- self.attString = nil;
- }else{
- self.attString = [[[NSMutableAttributedString alloc] initWithString:text] autorelease];
- }
- }
- // 设置某段字的颜色
- - (void)setColor:(UIColor *)color fromIndex:(NSInteger)location length:(NSInteger)length{
- if (location < 0||location>self.text.length-1||length+location>self.text.length) {
- return;
- }
- [_attString addAttribute:(NSString *)kCTForegroundColorAttributeName
- value:(id)color.CGColor
- range:NSMakeRange(location, length)];
- }
- // 设置某段字的字体
- - (void)setFont:(UIFont *)font fromIndex:(NSInteger)location length:(NSInteger)length{
- if (location < 0||location>self.text.length-1||length+location>self.text.length) {
- return;
- }
- [_attString addAttribute:(NSString *)kCTFontAttributeName
- value:(id)CTFontCreateWithName((CFStringRef)font.fontName,
- font.pointSize*2,
- NULL)
- range:NSMakeRange(location, length)];
- }
- // 设置某段字的风格
- - (void)setStyle:(CTUnderlineStyle)style fromIndex:(NSInteger)location length:(NSInteger)length{
- if (location < 0||location>self.text.length-1||length+location>self.text.length) {
- return;
- }
- [_attString addAttribute:(NSString *)kCTUnderlineStyleAttributeName
- value:(id)[NSNumber numberWithInt:style]
- range:NSMakeRange(location, length)];
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect
- {
- // Drawing code
- }
- */
- @end
|