[英]Unable to set Title of UIButton
我是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];
}
我能夠看到按鈕,但無法在UIButton上看到我的文字。
任何幫助將不勝感激。
請設置按鈕背景圖像。您已經設置了按鈕圖像。 請使用此:
[button setBackgroundImage:[UIImage imageNamed:@"blue_button.png"]
forState:UIControlStateNormal];
對於包裝按鈕標題用戶:
[button setTitle:@"Test button name 123" forState:UIControlStateNormal];
button.titleLabel.numberOfLines=2;
button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
您看不到該文本,因為您使用過按鈕的setImage:函數。 一次您可以在文字上顯示圖片或按鈕,但可以將圖片添加為背景,以便同時看到圖片和標題。 簡而言之,將圖像設置為背景。
[btn setBackgroundImage:[UIImage imageNamed:@"imageName"] forState:UIControlStateNormal];
還請刪除設置插圖行。
而是設置
[button setImage: [UIImage imageNamed:@"ibook2.png"] forState:UIControlStateNormal]; // This will not allow you to set Button title.
您必須使用:
[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.