본문 바로가기
Programming/iOS

UILabel 글씨에 테두리선 그리기.

by 신규하 2011. 6. 10.

UILabel의 글씨에 테두리선 그리기 입니다.





UIOutlineLabel.h
#import <Foundation/Foundation.h>


@interface UIOutlineLabel : UILabel {
    UIColor *outlineColor;
    CGFloat outlineDeep;
}

@property (nonatomic, retain)     UIColor *outlineColor;
@property (nonatomic) CGFloat outlineDeep;

@end


UIOutlineLabel.m
#import "UIOutlineLabel.h"

@implementation UIOutlineLabel
@synthesize outlineColor, outlineDeep;

- (id) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.outlineColor = [UIColor blackColor];
        self.outlineDeep = 3;
    }
    return self;
}

-(void)drawTextInRect:(CGRect)rect {
   
    CGSize shadowOffset = self.shadowOffset;
    UIColor *textColor = self.textColor;
   
    CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(c, self.outlineDeep);
    CGContextSetLineJoin(c, kCGLineJoinRound);
   
    CGContextSetTextDrawingMode(c, kCGTextStroke);
    self.textColor = self.outlineColor;
    [super drawTextInRect:rect];
   
    CGContextSetTextDrawingMode(c, kCGTextFill);
    self.textColor = textColor;
    self.shadowOffset = CGSizeMake(0, 0);
    [super drawTextInRect:rect];
   
    self.shadowOffset = shadowOffset;
}

@end

사용법
    UIOutlineLabel *test = [[[UIOutlineLabel alloc] initWithFrame:CGRectMake(10, 10, 200, 30)] autorelease];
    test.text = @"Outline Test";
    test.outlineDeep  = 3;
    test.textColor = [UIColor whiteColor];
    test.outlineColor = [UIColor redColor];
    test.textAlignment = UITextAlignmentCenter;
    [self.view addSubview:test];


댓글