0

ラベル プロパティと背景画像を使用してプログラムで生成された 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
4

2 に答える 2

2

setTitleColor:forState:を呼び出しUIControlStateHighlightedます。ドキュメント。

于 2013-06-12T03:55:29.610 に答える
0

これらの 2 つの同様の質問を確認してください。彼らはあなたの質問を確実に解決します:

質問1

質問2

試してみる 1 つのことを次に示します。

likeのUIControlStateHighlighted状態のターゲットを追加できますUIButton

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

メソッドでbuttonHighlightedは、ラベルのテキストの色を変更できます

- (void) buttonHighlighted:(id)sender 
{
  //code to change the color of your Label subview...  
}

お役に立てれば

于 2013-06-12T04:11:11.197 に答える