简体   繁体   English

无法设置UIButton的标题

[英]Unable to set Title of UIButton

I am new to iPhone, 我是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. 我能够看到按钮,但无法在UIButton上看到我的文字。

Any help will be appreciated. 任何帮助将不胜感激。

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;

You can't see the text because you have used setImage: function of button. 您看不到该文本,因为您使用过按钮的setImage:函数。 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. 还请删除设置插图行。

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.

如果图像太大,则仅会显示图像,并且文本会由于图像而被截断,因为文本在图像后将可见,请尝试使用setBackground图像或尝试使用小分辨率图像或setImageEdgeInsets。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM