簡體   English   中英

UILabel子類不顯示文本

[英]UILabel subclass doesn't show text

我已經繼承了UILabel類,以重寫drawRect:方法。 IB中的標簽與創建的子類連接。 它出現在屏幕上,但是無論它是在IB還是以編程方式設置,都不會顯示任何文本。 標簽本身出現在屏幕上,當我記錄它的text屬性時,它顯示OK。

這是我的代碼:

我的標簽

#import <UIKit/UIKit.h>
@interface MyLabel : UILabel
@end

我的標簽

#import "MyLabel.h"
#import <QuartzCore/QuartzCore.h>

@implementation MyLabel


- (void)drawRect:(CGRect)rect {
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = UIGraphicsGetCurrentContext();

    UIColor *borderColor = [UIColor colorWithWhite: .1f alpha: 5.f];
    UIColor *topColor = [UIColor colorWithWhite: .3f alpha: 5.f];
    UIColor *bottomColor = [UIColor colorWithWhite: .5f alpha: 5.f];
    UIColor *innerGlow = [UIColor colorWithWhite: 1.f alpha: .25f];

    NSArray *gradientColors = @[(id)topColor.CGColor, (id)bottomColor.CGColor];
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) gradientColors,  NULL);

    CGFloat bWidth = self.frame.size.width;
    CGFloat bHeight = self.frame.size.height;

    UIBezierPath *roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect:    CGRectMake(0, 0, bWidth, bHeight)
                                                                cornerRadius: 0];
    [roundedRectanglePath addClip];

    CGGradientRef background = gradient;

    CGContextDrawLinearGradient(context, background, CGPointMake(bWidth / 2.f, 0), CGPointMake(bWidth / 2.f, bHeight), 0);

    [borderColor setStroke];
    roundedRectanglePath.lineWidth = 6;
    [roundedRectanglePath stroke];

    CGFloat glowRadius = 3.f;
    UIBezierPath *innerGlowRect = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(glowRadius, glowRadius, bWidth - 2 * glowRadius, bHeight - 2 * glowRadius)   cornerRadius: 0];
    [innerGlow setStroke];
    innerGlowRect.lineWidth = 3;
    [innerGlowRect stroke];

    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorSpace);
}

@end

也許我需要重寫drawTextInRect:方法,但是我不知道怎么做。 感謝幫助!

您需要致電:

[super drawRect:rect];

在頂部或您的drawRect:方法。 如果您需要對字符串的外觀進行更多控制,則需要使用NSString字符串繪制方法自行繪制

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM