2

I am new to iPhone,

    xpos=30;
    ypos=40;

for(int i=0;i<[MyArray count];i++)
        {            
            if(i!=0)
            {
                if (i%4==0) 
                {                    
                    ypos+=180;
                    xpos=30;
                }
                else
                {
                    xpos+=200;
                }
            }
            
            button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(xpos, ypos, 110.0, 130.0);
            [button setImage: [UIImage imageNamed:@"ibook2.png"] forState:UIControlStateNormal];
            [button setTitleEdgeInsets:UIEdgeInsetsMake(ypos, xpos, 0.0, 0.0)];
            [button setTitle:[NSString stringWithFormat:@"%@", [MyArray objectAtIndex:i]] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            [self.view addSubview:button];
}

i am able to see button but unable to see my text on the UIButton.

Any help will be appreciated.

Community
  • 1
  • 1
Krunal
  • 6,440
  • 21
  • 91
  • 155
  • I think your text on the button is not visible because of your xpos and ypos. It may be out of focus. – Prabha Jul 23 '12 at 06:42
  • remove this > [button setTitleEdgeInsets:UIEdgeInsetsMake(ypos, xpos, 0.0, 0.0)]; and try agian. – waheeda Jul 23 '12 at 06:43
  • Your title insets seem to be much bigger than the size of the button. That gives you no available pixels for the title inside te button. – David Rönnqvist Jul 23 '12 at 06:43
  • No all is because of setImage function. See my reply. Inset could be issue but real issue is not that one. – Iducool Jul 23 '12 at 06:43
  • Well, we can set title and image both in a uibutton.. thats not an issue. – waheeda Jul 23 '12 at 06:50
  • @waheeda: Of course you can set but not with using setImage function. if you want both things then you have to set image as a background of button. Check below comments of Krunal that issue is partially fixed but now issue is in text wrapping. – Iducool Jul 23 '12 at 06:57
  • @Armaan: :) just FYI, we CAN set title along with image in a button (and with setImage method), I have done it many times. here is the proof: http://stackoverflow.com/questions/4344847/iphone-sdk-uibutton-with-both-image-and-text-possible – waheeda Jul 23 '12 at 07:10
  • 1
    @waheeda:really thankful to you. You taught me something new :) – Iducool Jul 23 '12 at 07:14

4 Answers4

4

Please set buttons background image.You have set buttons image. Please use this:

[button setBackgroundImage:[UIImage imageNamed:@"blue_button.png"]
                            forState:UIControlStateNormal];

For Wrapping button title User:

[button setTitle:@"Test button name 123" forState:UIControlStateNormal];

 button.titleLabel.numberOfLines=2;

 button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
Dhruv
  • 2,153
  • 3
  • 21
  • 45
Hemant Dixit
  • 1,145
  • 8
  • 12
2

You can't see the text because you have used setImage: function of button. At a time you can display image or button on text but you can add image as a background so you can see both image and tittle. In short set image as a background.

[btn setBackgroundImage:[UIImage imageNamed:@"imageName"] forState:UIControlStateNormal];

Please also remove the line of setting inset.

Iducool
  • 3,543
  • 2
  • 24
  • 45
  • I tried using `setBackgroundImage` it is working fine but text is getting wrapped, i do not want wrapping. – Krunal Jul 23 '12 at 06:51
  • unable to see due to cyber roam :) – Iducool Jul 23 '12 at 06:59
  • i tried this also, button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(xpos, ypos, 110.0, 130.0); [button setImage:[UIImage imageNamed:@"ibook2.png"] forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown]; – Krunal Jul 23 '12 at 07:06
  • i am not sure why your image is looks like squeeze and text is going outside of the button. – Iducool Jul 23 '12 at 07:07
  • booklbl = [[[UILabel alloc] initWithFrame:CGRectMake(xpos, ypos, 70, 80)]autorelease]; booklbl.lineBreakMode = UILineBreakModeWordWrap; booklbl.numberOfLines = 0; booklbl.backgroundColor = [UIColor blueColor]; booklbl.text =@"222";[button addSubview:booklbl]; [self.view addSubview:button]; – Krunal Jul 23 '12 at 07:08
  • Why are you adding label into button there is already built in button available. ex. btn.titleLabel. One more thing you should have to calculate dynamic frame of label according to text. – Iducool Jul 23 '12 at 07:12
0

Rather then setting

[button setImage: [UIImage imageNamed:@"ibook2.png"] forState:UIControlStateNormal];   // This will not allow you to set Button title.

You have to use :

[button setBackgroundImage: [UIImage imageNamed:@"ibook2.png"] forState:UIControlStateNormal];    // This will allow you to set title along background button image.
Devang
  • 11,258
  • 13
  • 62
  • 100
0

if you image is too big then it will only show the image and text get truncated due to image as text will be visible after image, try using setBackground image or try small resolution image or setImageEdgeInsets .

Manish Agrawal
  • 10,958
  • 6
  • 44
  • 76