Saturday, June 18, 2011

First few methods to create buttons/labels (Updated)

I was always quite annoyed to have to write so much code just to create simple button or label --- as I don't like to use Interface Builder.

Thanks to this post from iPhone Dev SDK Forum, I sort of worked out how to create a utility/shared function for creating buttons and labels as below. Of course could have further improve it to provide further options in setting colours for different parts and other stuffs, but this is sufficient for what I am working on now.

Note that in the last line for "createNormalButton", at first I used "[buttonObj autorelease]". But I found that the buttons will just display a blank box with white colour, no title at all and couldn't click on it either. After changed to "[[buttonObj retain]autorelease]" it seems to work ok now. Not sure if this will cause any memory leak..... Please let me know if any of you got better ideas/suggestions.

+ (void) createNormalButton: (UIButton *) buttonObj 
 atPosX: (double) buttonPositionX
 atPosY: (double) buttonPositionY   
                  withWidth: (double) buttonWidth  
                 withHeight: (double) buttonHeight 
                withBGColor: (UIColor *) buttonBGColor 
             withTitleColor: (UIColor *) buttonTitleColor
withTag: (int) buttonTag 
withTitle: (NSString *) buttonTitle 
withSelfID: (id)buttonSelfID 
withActionID: (SEL)selectorID 
                  ifEnabled: (BOOL)buttonEnabled 
                     inView: (UIView *)viewToAddTo

 {
    
buttonObj = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonObj setFrame:CGRectMake(buttonPositionX, buttonPositionY, buttonWidth, buttonHeight)]; 
[buttonObj setTitle: buttonTitle forState:UIControlStateNormal];
[buttonObj setTitleShadowColor:buttonBGColor forState:UIControlStateNormal];
        [buttonObj setBackgroundColor: buttonBGColor];
[buttonObj setTag: buttonTag];
    
        [buttonObj addTarget:buttonSelfID action:selectorID      
            forControlEvents:UIControlEventTouchUpInside];

    if (buttonEnabled) {
        [buttonObj setEnabled:YES];
        [buttonObj setTitleColor:buttonTitleColor forState:UIControlStateNormal];
    } else {
        [buttonObj setEnabled:NO];
        [buttonObj setTitleColor:buttonBGColor forState:UIControlStateNormal];
    }

[viewToAddTo addSubview:buttonObj];
        [[buttonObj retain ]autorelease];
}


+(void) createNormalLabel: (UILabel *) labelObj 
                   atPosX: (double)labelPositionX 
                   atPosY: (double)labelPositionY  
                withWidth: (double)labelWidth 
               withHeight: (double)labelHeight                 
              withBGColor: (UIColor *) labelBGColor  
            withTextColor: (UIColor *) labelTextColor  
                  withTag: (int) labelTag                  
                 withText: (NSString *)labelText
                   inView: (UIView *)viewToAddTo
{
    labelObj = [[UILabel alloc] initWithFrame:CGRectMake(labelPositionX, labelPositionY, labelWidth, labelHeight)];
    
[labelObj setTag:labelTag];
[labelObj setText:labelText];
[labelObj setBackgroundColor:labelBGColor];

 [labelObj setTextColor:labelTextColor];
[labelObj setNumberOfLines:0];
[labelObj sizeToFit];
[viewToAddTo addSubview:labelObj];
[labelObj release];
}

With these methods, I can now simplify the button creation code to as below. Will need to work out a more organised way of controlling the Position X/Y values somehow later.


    UIButton *buttonNext;
    UIButton *buttonPrev;
    [SharedFunctions createNormalButton: buttonNext 
                                 atPosX: 180 
                                 atPosY: 420 
                              withWidth: 80
                             withHeight: 30  
                            withBGColor: CONST_COLOR_BACKGROUND 
                         withTitleColor: CONST_COLOR_BUTTON_TITLE 
                                withTag: TAG_BUTTON_NEXT 
                              withTitle: @"Next" 
                             withSelfID: self 
                           withActionID: @selector(buttonNextClicked) 
                              ifEnabled: YES
                                 inView: self.view];
   
    [SharedFunctions createNormalButton: buttonPrev     
                                 atPosX: 60 
                                 atPosY: 420 
                              withWidth: 80
                             withHeight: 30                               
                            withBGColor: CONST_COLOR_BACKGROUND 
                         withTitleColor: CONST_COLOR_BUTTON_TITLE 
                                withTag: TAG_BUTTON_PREVIOUS 
                              withTitle: @"Previous" 
                             withSelfID: self 
                           withActionID: @selector(buttonPreviousClicked) 
                              ifEnabled: NO
                                 inView: self.view];

For labels, it's like this:

    [SharedFunctions createNormalLabel: test1Label 
                                atPosX: 10 
                                atPosY: 200 
                             withWidth: 300 
                            withHeight: 300 
                           withBGColor: CONST_COLOR_BACKGROUND  
                         withTextColor: CONST_COLOR_LABEL_TEXT 
                               withTag: TAG_LABEL_TEST1 
                              withText@"Click on the previous/next buttons to navigate" 
                                inView: self.view];

Hope this helps!

Update: Missed a line about setting text color for the label, fixed now.

0 comments:

Post a Comment