Ios UILabel子类不显示文本

Ios UILabel子类不显示文本,ios,objective-c,uilabel,subclass,Ios,Objective C,Uilabel,Subclass,我已经对UILabel类进行了子类化,以覆盖drawRect:method。IB中的标签与创建的子类连接。它出现在屏幕上,但不显示任何文本,无论是在IB中设置的还是以编程方式设置的。标签本身出现在屏幕上,当我记录它的文本属性时,它显示OK 这是我的密码: MyLabel.h #import <UIKit/UIKit.h> @interface MyLabel : UILabel @end MyLabel.m #import "MyLabel.h" #import <Quart

我已经对UILabel类进行了子类化,以覆盖drawRect:method。IB中的标签与创建的子类连接。它出现在屏幕上,但不显示任何文本,无论是在IB中设置的还是以编程方式设置的。标签本身出现在屏幕上,当我记录它的文本属性时,它显示OK

这是我的密码:

MyLabel.h

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

#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:method,但我不知道如何重写。 谢谢你的帮助

您需要拨打:

[super drawRect:rect];
在顶部或您的drawRect:method。如果您需要更多地控制字符串的外观,则需要使用

您需要拨打:

[super drawRect:rect];

在顶部或您的drawRect:method。如果您需要更多地控制字符串的外观,则需要使用

是的,这很有效!非常感谢。我知道,如果要对UIView进行子类化,就不应该调用super方法,但没有注意到超类是UILabel@NikitaShytyk为什么不在UIView子类上调用super,而您的类的super类可能是UILabel,但UILabel的super类是UIView。我正在尝试使用IBDesignable for UILabel子类,并像这样重写drawRect,但即使我已经放置了[super-drawRect:rect];,也不会出现任何文本;。怎么了?是的,这很有效!非常感谢。我知道,如果要对UIView进行子类化,就不应该调用super方法,但没有注意到超类是UILabel@NikitaShytyk为什么不在UIView子类上调用super,而您的类的super类可能是UILabel,但UILabel的super类是UIView。我正在尝试使用IBDesignable for UILabel子类,并像这样重写drawRect,但即使我已经放置了[super-drawRect:rect];,也不会出现任何文本;。怎么了?