UILabel 不会随按钮变暗

Posted

技术标签:

【中文标题】UILabel 不会随按钮变暗【英文标题】:UILabel doesn't dim with button 【发布时间】:2013-06-12 03:47:27 【问题描述】:

我有一个以编程方式生成的带有标签属性和背景图像的 UIButton。轻按时,背景图像变为灰色,但文本不会。如何让标签在被点击时也显示?

根据要求,我的按钮代码:

#import "BCB.h"
@implementation BCB
@synthesize cardText;
- (id)initWithFrame:(CGRect)frame

    self = [super initWithFrame:frame];
    if (self)
    
        [self setFrame:frame];
        cardText = cardText = [[MyLabel alloc]
                               initWithFrame:(CGRect)8, 8, 144, 186];
        cardText.numberOfLines = 0;
        cardText.font = [UIFont fontWithName:@"Helvetica" size:18.0];
        cardText.textColor = [UIColor whiteColor];
        cardText.backgroundColor = [UIColor clearColor];
        cardText.lineBreakMode = NSLineBreakByWordWrapping;
        [self addSubview:cardText];
        [self setBackgroundImage:[UIImage imageNamed:@"BC" ] forState:UIControlStateNormal];
    
    return self;

@end

还有 MyLabel 代码:

#import "MyLabel.h"
@implementation MyLabel
// Helper function to actually set the text.
- (void)reallySetText:(NSString*)text

    [super setText:text];

// Function to top align text within a label.
- (void)topAlign

    // Get the size of the text box containing the label with its current font and size.
    CGSize textSize = [self.text sizeWithFont:self.font
                            constrainedToSize:(CGSize)self.frame.size.width, self.frame.size.height
                                lineBreakMode:NSLineBreakByWordWrapping];
    // Trim the frame up to exactly fit the text.
    self.frame = (CGRect)self.frame.origin.x, self.frame.origin.y, textSize.width, textSize.height;

// Shrink font until it fits the provided frame.
CGFloat maxSizeThatFitsFrame(MyLabel* labelCopy, CGFloat fontSize, CGRect rect)

    while (labelCopy.frame.size.height > rect.size.height && fontSize > 4.0f)
    
        fontSize -= 0.5f;
        labelCopy.font = [UIFont fontWithName:@"Helvetica" size:fontSize];
        [labelCopy topAlign];
    
    return fontSize;

// Overloading the drawTextInRect method to topAlign the text as well.
-(void)drawTextInRect:(CGRect)rect

    [super drawTextInRect:rect];
    [self topAlign];

// Function to set the label to have the provided text within the provided rectangle with the provided initial font size.
- (void)setText:(NSString *)text inFrame:(CGRect)rect withFontSize:(CGFloat)fontSize andColor:(UIColor*)color

    // Set the text.
    [super setText:text];
    // Set the frame.
    [self setFrame:rect];
    // Make a copy of the label.
    MyLabel* labelCopy = [[MyLabel alloc]
                          initWithFrame:
                          (CGRect)
                              self.frame.origin.x,
                              self.frame.origin.y,
                              self.frame.size.width,
                              self.frame.size.height+240];
    // Make the text wrap properly.
    labelCopy.numberOfLines = 0;
    labelCopy.lineBreakMode = NSLineBreakByWordWrapping;
    // Set the copy's text.
    [labelCopy reallySetText:self.text];
    // Set the copy's font.
    labelCopy.font = [UIFont fontWithName:@"Helvetica" size:fontSize];
    // Top align the copy.
    [labelCopy topAlign];
    // If the copy's height is larger than the rectangle's height, resize it until it fits.
    if (labelCopy.frame.size.height > rect.size.height)
    
        fontSize = maxSizeThatFitsFrame(labelCopy, fontSize, rect);
    
    // Set the label's font to a size that fits.
    [self setFont:[UIFont fontWithName:@"Helvetica" size:fontSize]];
    // Set the label's color.
    self.textColor = color;
    // Make the text wrap properly.
    self.numberOfLines = 0;
    self.lineBreakMode = NSLineBreakByWordWrapping;
    // Top align the label.
    [self topAlign];

@end

【问题讨论】:

【参考方案1】:

致电setTitleColor:forState: 获取UIControlStateHighlighted。 Docs.

【讨论】:

当标题不是由[button setTitle:]设置的时,此方法是否有效?【参考方案2】:

看看这两个类似的问题。他们一定会解决您的疑问:

Question 1

Question 2

这里有一件事可以尝试:

您可以为UIControlStateHighlighted 状态的UIButton 添加目标

[button addTarget:self action:@selector(buttonHighlighted:) forControlEvents:UIControlStateHighlighted];

buttonHighlighted 方法中,您可以更改标签文本的颜色

- (void) buttonHighlighted:(id)sender 

  //code to change the color of your Label subview...  

希望对你有帮助

【讨论】:

以上是关于UILabel 不会随按钮变暗的主要内容,如果未能解决你的问题,请参考以下文章

UILabel 不会改变屏幕

当按下自定义按钮时,UILabel 不会更新(Objective-C)

按下按钮时未从数组中填充 UILabel

Swift:UILabel 上的 UIButton

如何防止 UILabel 在 Swift 中阻止对父 UIButton 的触摸?

NGUI如何让uilabel框大小随文字增加而变化