2017-01-17 6 views
0

UILabel을 하위 클래스로 만들려고합니다.UILabel Subclass Text가 사라짐

코드 .H

#import <UIKit/UIKit.h> 

IB_DESIGNABLE 

@interface PercentLabel : UILabel 
@property (nonatomic) IBInspectable CGFloat ratio; 
@property (nonatomic) IBInspectable UIColor *color1; 
@property (nonatomic) IBInspectable UIColor *color2; 


- (void)drawRect:(CGRect)rect; 

@end 

코드하는 .m

#import "PercentLabel.h" 

@implementation PercentLabel 

- (void)drawRect:(CGRect)rect { 
    // Drawing code 
    [super drawRect:rect]; 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetFillColorWithColor(context, [_color1 CGColor]); 
    CGContextFillRect(context, rect); 

} 


@end 

사용자 지정 속성이 스토리 보드에서 잘 작동하지만, Rectfill 내가 가진 텍스트를 포함 보인다. 예를 들어 불투명도 0.5로 색상을 설정하면 색상 뒤에 텍스트가 표시됩니다.

누군가가 이것이 왜 그렇게하고 텍스트를 맨 위에 표시하도록 수정해야하는지 설명 할 수 있습니까?

+0

이미 해결책을 찾았습니다. 그냥 [super drawRect : rect]를 넣어야합니다. 그리기 후. Lulzz 멍청한 녀석들이 사람들을 부르짖어. – GeneCode

답변

0

내가 한 것은 그림 코드의 끝 부분에 슈퍼를 넣어 두었습니다.

- (void)drawRect:(CGRect)rect { 
    // Drawing code 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetFillColorWithColor(context, [_color1 CGColor]); 
    CGContextFillRect(context, rect); 
    [super drawRect:rect]; //<--- 

} 

슈퍼는 기본적으로 UILabel의 기본 렌더링을 그립니다. 따라서이 경우에는 텍스트 그리기가 포함됩니다.