更新:更正了按钮属性的文本和图像。
我正在尝试显示一个按钮,如果有特定信息,并且点击时该按钮执行操作;否则,只显示标签。我创建了以下内容:
if(rec.biography != nil)
{
btnProviderBio = [UIButton buttonWithType:UIButtonTypeCustom];
[btnProviderBio setImage:[UIImage imageNamed:@"info.png"] forState:UIControlStateNormal];
[btnProviderBio setTitle:[NSString stringWithFormat:@"%@ - %@", rec.providerName, rec.facilityName] forState:UIControlStateNormal];
[btnProviderBio setFrame: CGRectMake(13.0, yValue, [UIScreen mainScreen].bounds.size.width-26, 40.0)];
[btnProviderBio addTarget:self action:@selector(providerAlertDiaglog:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnProviderBio];
}
else{
lblFacilityProvider = [[UILabel alloc] init];
[lblFacilityProvider setFont:[UIFont systemFontOfSize: 14]];
[lblFacilityProvider setText:[NSString stringWithFormat:@"%@ - %@", rec.providerName, rec.facilityName]];
[lblFacilityProvider setFrame: CGRectMake(13.0, yValue, [UIScreen mainScreen].bounds.size.width-26, 40.0)];
[self.view addSubview:lblFacilityProvider];
}
我现在得到以下结果。没有标签文本和随机位置的按钮。我希望按钮位于左侧,文本在右侧。
答案 0 :(得分:0)
我认为你应该使用setTitleTintColor,因为Custom按钮的默认titleTintColor值是白色的,所以你需要设置titleTintColor。 请查看以下代码。
if(rec.biography != nil)
{
btnProviderBio = [UIButton buttonWithType:UIButtonTypeCustom];
[btnProviderBio setImage:[UIImage imageNamed:@"info.png"] forState:UIControlStateNormal];
[btnProviderBio setTitle:[NSString stringWithFormat:@"%@ - %@", rec.providerName, rec.facilityName] forState:UIControlStateNormal];
[btnProviderBio setFrame: CGRectMake(13.0, yValue, [UIScreen mainScreen].bounds.size.width-26, 40.0)];
[btnProviderBio addTarget:self action:@selector(providerAlertDiaglog:) forControlEvents:UIControlEventTouchUpInside];
// I used rgb color but you can use your color, this rgb color just for your reference.
[btnProviderBio setTitleColor:[UIColor colorWithRed:36/255.0 green:71/255.0 blue:113/255.0 alpha:1.0] forState:UIControlStateNormal];
[self.view addSubview:btnProviderBio];
}
else{
lblFacilityProvider = [[UILabel alloc] init];
[lblFacilityProvider setFont:[UIFont systemFontOfSize: 14]];
[lblFacilityProvider setText:[NSString stringWithFormat:@"%@ - %@", rec.providerName, rec.facilityName]];
[lblFacilityProvider setFrame: CGRectMake(13.0, yValue, [UIScreen mainScreen].bounds.size.width-26, 40.0)];
[self.view addSubview:lblFacilityProvider];
}