2017-01-17 50 views
0

我想要繼承UILabel。UILabel子類文本消失

代碼.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 noob片刻人。 – GeneCode

回答

0

我所做的是超級繪圖代碼的結束。

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

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

} 

超級基本上繪製了UILabel的默認渲染。所以在這種情況下,包括繪製文字。