create uibutton subclass

前端 未结 5 1718
说谎
说谎 2020-11-28 14:10

I tried to subclass UIButton to include an activity indicator, but when i use initWithFrame:(since i\'m subclassing uibutton i\'m not using buttonWithType:) the button doesn

5条回答
  •  再見小時候
    2020-11-28 14:29

    I have created a custom class, preferring composition over inheritance and it works perfect. My custom class has a button and it knows it's MCContact object. Also it draws a proper button and calculates frames automatically using MCContact object, that is passed.

    Header file sample:

    #import 
    
    @protocol MCContactViewDelegate;
    
    @interface MCContactView : UIView
    {
    
    }
    
    @property (nonatomic, strong) MCContact *mcContact;
    @property (nonatomic, weak) id  delegate;
    
    - (id)initWithContact:(MCContact*)mcContact delegate:(id )delegate;
    
    @end
    
    @protocol MCContactViewDelegate 
    
    - (void)contactViewButtonClicked:(MCContactView*)contactView;
    
    @end
    

    Implementation file:

    #import "MCContactView.h"
    
    @interface MCContactView()
    {
        UIButton *_button;
    }
    
    @end
    
    @implementation MCContactView
    
    - (id)initWithContact:(MCContact*)mcContact delegate:(id )delegate
    {
        self = [super initWithFrame:CGRectZero];
    
        if (self) {
    
            GetTheme();
    
            _mcContact = mcContact;
            _delegate = delegate;
            _button = [UIButton buttonWithType:UIButtonTypeCustom];
    
            UIImage *normalBackgroundImage = [[UIImage imageNamed:@"tokenNormal.png"] stretchableImageWithLeftCapWidth:12.5 topCapHeight:12.5];
            [_button setBackgroundImage:normalBackgroundImage forState:UIControlStateNormal];
    
            UIImage *highlightedBackgroundImage = [[UIImage imageNamed:@"tokenHighlighted.png"] stretchableImageWithLeftCapWidth:12.5 topCapHeight:12.5];
            [_button setBackgroundImage:highlightedBackgroundImage forState:UIControlStateHighlighted];
    
            _button.titleLabel.font = [theme contactButtonFont];
            [_button setTitleColor:[theme contactButtonTextColor] forState:UIControlStateNormal];
    
            [_button setTitleEdgeInsets:UIEdgeInsetsMake(4, 6, 4, 6)];
    
            NSString *tokenString = ([allTrim(mcContact.name) length]>0) ? mcContact.name : mcContact.eMail;
            [_button setTitle:tokenString forState:UIControlStateNormal];
    
            [_button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
            CGSize size = [tokenString sizeWithFont:[theme contactButtonFont]];
            size.width += 20;
            if (size.width > 200) {
                size.width = 200;
            }
            size.height = normalBackgroundImage.size.height;
            [_button setFrame:CGRectMake(0, 0, size.width, size.height)];
    
            self.frame = _button.frame;
            [self addSubview:_button];
        }
    
        return self;
    }
    
    
    - (void)buttonClicked:(id)sender
    {
        [self.delegate contactViewButtonClicked:self];
    }
    
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect
    {
        // Drawing code
    }
    */
    
    @end 
    

提交回复
热议问题