gpt4 book ai didi

iphone - 使用 drawRect 为 UIButton 的标签添加下划线

转载 作者:行者123 更新时间:2023-12-01 18:28:14 24 4
gpt4 key购买 nike

我正在尝试在 UIButton 中为标签的文本添加下划线。但这对我不起作用。
我的代码在 viewDidLoad方法:

    CGFloat x = 13.0f;
CGFloat y = 15.0f;
if (self.numberOfFirstButton == 0) {
self.numberOfFirstButton = 1;
}
for (NSUInteger i = 1; i <= 5; i++) {

for (NSUInteger j = 0; j < 10; j++) {
UIHouseButtons *button = [[UIHouseButtons alloc] init];
[button setFrame:CGRectMake(x+(47*j), y, 30.0f, 30.0f)];
[button setTitle:[NSString stringWithFormat:@"%i", self.numberOfFirstButton] forState:UIControlStateNormal];
[button drawRect:CGRectMake(x+(47*j), y, 30.0f, 30.0f)];
[self.view addSubview:button];
self.numberOfFirstButton++;
}
y += 48.0f;
}

我的 drawRect方法
- (void)drawRect:(CGRect)rect
{
CGRect textRect = self.titleLabel.frame;

// need to put the line at top of descenders (negative value)
CGFloat descender = self.titleLabel.font.descender;

CGContextRef contextRef = UIGraphicsGetCurrentContext();

// set to same colour as text
CGContextSetStrokeColorWithColor(contextRef, self.titleLabel.textColor.CGColor);

CGContextMoveToPoint(contextRef, textRect.origin.x, textRect.origin.y + textRect.size.height + descender);

CGContextAddLineToPoint(contextRef, textRect.origin.x + textRect.size.width, textRect.origin.y + textRect.size.height + descender);

CGContextClosePath(contextRef);

CGContextDrawPath(contextRef, kCGPathStroke);
[super drawRect:rect];
}

我的所有按钮都在 View 中,但标签的文本没有下划线。我做错了什么?

最佳答案

您可以从 UILabel 继承并覆盖 drawRect 方法:

 - (void)drawRect:(CGRect)rect {

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(ctx, 207.0f/255.0f, 91.0f/255.0f, 44.0f/255.0f, 1.0f); // RGBA
CGContextSetLineWidth(ctx, 1.0f);

CGContextMoveToPoint(ctx, 0, self.bounds.size.height - 1);
CGContextAddLineToPoint(ctx, self.bounds.size.width, self.bounds.size.height - 1);

CGContextStrokePath(ctx);

[super drawRect:rect];
}
然后在该标签上放置一个自定义按钮。

关于iphone - 使用 drawRect 为 UIButton 的标签添加下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11214893/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com