ios - 填充 uilabel 的渐变时文本消失,自定义 uilabel

标签 ios objective-c core-graphics uilabel

我的目标是通过执行以下操作为我的 uilabel 添加渐变(CustomLabelBackGround 是 UILabel 的子类)

@implementation CustomLabelBackGround

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
    }
    return self;
}

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

    CGColorRef whiteColor       =   [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0].CGColor;
    CGColorRef lightGrayColor   =   [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0].CGColor;
    CGColorRef separatorColor   =   [UIColor colorWithRed:208.0/255.0 green:208.0/255.0 blue:208.0/255.0 alpha:1.0].CGColor;

    CGRect paperRect            =   self.bounds;

    // Fill with gradient
    drawLinearGradient(context, paperRect, whiteColor, lightGrayColor);

    // Add white 1 px stroke
    CGRect strokeRect           =   paperRect;
    strokeRect.size.height     -=   1;
    strokeRect                  =   rectFor1PxStroke(strokeRect);

    CGContextSetStrokeColorWithColor(context, whiteColor);
    CGContextSetLineWidth(context, 1.0);
    CGContextStrokeRect(context, strokeRect);

    // Add separator
    CGPoint startPoint          =   CGPointMake(paperRect.origin.x, paperRect.origin.y + paperRect.size.height - 1);
    CGPoint endPoint            =   CGPointMake(paperRect.origin.x + paperRect.size.width - 1, paperRect.origin.y + paperRect.size.height - 1);
    draw1PxStroke(context, startPoint, endPoint, separatorColor);

}

但是,当我尝试在屏幕上显示时,CustomLabelBackGround 的文本消失了。请看下图作为引用:

enter image description here

我在这里缺少什么。如果您对此有任何想法,请提供帮助。谢谢

最佳答案

您需要调用:

[super drawRect:rect];

为了在绘制其他任何内容之前 super 绘制文本。然后正常绘制文本

关于ios - 填充 uilabel 的渐变时文本消失,自定义 uilabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15772630/

相关文章:

ios - 如何在UIpickerView的特定行中插入图像和文本?

ios - 如果我从可重用单元格的 super View 中删除 UILabel,重用时如何重新初始化单元格中的 UILabel?

iphone - 我可以检测 UIPickerView 滚动事件吗?

ios - 调整大小时如何调整适合SKSpriteNode内部的子节点?

swift - 平滑的 UIBezierPath

iphone - UIImageJPEGRepresentation() 线程安全吗?

ios - 在 iMessage 应用程序的对话中存储变量

ios - setNeedsDisplay 不适用于 UIImageView 的子类

ios - AFNetworking 2.0 将 NSURLSessionDataTask 转换为 NSURLSessionDownloadTask 不会将所有文件数据写入磁盘

objective-c - 使用 CoreGraphics 绘制圆角线性渐变(或扩展径向渐变)