UILabel子类不显示文本

 谢海武181_160 发布于 2023-02-06 09:07

我已经将UILabel类子类化为覆盖drawRect:方法.IB中的标签与创建的子类相关联.它出现在屏幕上,但它不显示任何文本,无论是在IB中设置还是以编程方式设置.标签本身出现在屏幕上,当我记录它的text属性时,它显示OK.

这是我的代码:

MyLabel.h

#import 
@interface MyLabel : UILabel
@end

MyLabel.m

#import "MyLabel.h"
#import 

@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:方法,但我不知道如何.感谢帮助!

1 个回答
  • 你需要打电话:

    [super drawRect:rect];
    

    在顶部或您的drawRect:方法.如果你需要更多地控制字符串的外观,你需要使用NSString字符串绘制方法来绘制它.

    2023-02-06 09:12 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有