在应用UIBUtton的时候我们有时需要同时设置图片和文字,下面代码仅供参考:
UIButton *_backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_backButton setFrame:CGRectMake(12, 8, 64, 28)];
[_backButton setTitle:@"返回" forState:UIControlStateNormal]; //设置button在没有选中的时候显示的字体
_backButton.titleLabel.font = [UIFont systemFontOfSize:14.0f]; //设置button显示字体的大小
[_backButton setBackgroundImage:[UIImage imageNamed:@"backButton.png"] forState:UIControlStateNormal]; //设置button背景显示图片
[self.view addSubview:_backButton];
出了上面的操作意外,我们还可以同时设置button在选中(按下之后)以及正常状态下(没有选中)显示文字和图片的不同,
UIButton *_backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_backButton setFrame:CGRectMake(12, 8, 64, 28)];
[_backButton setTitle:@"返回" forState:UIControlStateNormal];
[_backButton setTitle:@"Down" forState:UIControlStateHighlighted];
[_backButton setBackgroundColor:[UIColor clearColor]];
[_backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_backButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];
[_backButton setBackgroundImage:[UIImage imageNamed:@"backButton.png"] forState:UIControlStateNormal];
[_backButton setBackgroundImage:[UIImage imageNamed:@"DownButton.png"] forState:UIControlStateNormal];
[_toolBar addSubview:_backButton];