2017-01-17 10 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のレンダリングを描画します。この場合、テキストの描画が含まれます。

関連する問題