我有一个尺寸宽200,高270的纽扣。我想有文字和图像在同一个按钮。而不是作为文本上的背景图像。相反,我希望在同一个按钮上显示高度为120的文本和高度为150的图像。该怎么做呢?
发布于 2010-12-03 21:17:57
你可以使用这段代码,它会满足你的需要
.h file code
IBOutlet UIButton *testBtn;
.m file code
[testBtn setImage: [UIImage imageNamed:@"Image name.png"] forState:UIControlStateNormal];
[testBtn setTitleEdgeInsets:UIEdgeInsetsMake(70.0, -150.0, 5.0, 5.0)];
[testBtn setTitle:@"Your text" forState:UIControlStateNormal];
调整按钮的坐标和大小,因为您的requirement.This是指导您的示例代码。
PS:在笔尖file>Make it a自定义button>Connect的IBOutlet到您的自定义按钮在笔尖file.Thats它。
发布于 2012-12-05 21:11:53
下面的代码显示了如何使用UIButton类的setImageEdgeInsets和setTitleEdgeInsets属性在按钮中放置图像和文本。
UIButton *butt=[UIButton buttonWithType:UIButtonTypeCustom ];
[butt setFrame:CGRectMake(0, 0, 100, 100)];
[butt setBackgroundImage:[UIImage imageNamed:@"sig.png"] forState:UIControlStateNormal];
[butt setImageEdgeInsets:UIEdgeInsetsMake(0.0, 0.0, 50.0, 0.0)];
[butt setTitleEdgeInsets:UIEdgeInsetsMake(75.0, 0.0, 0.0, 0.0)];
[butt setTitle:@"hello" forState:UIControlStateNormal];
[butt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.view addSubview:butt];
发布于 2010-12-03 19:10:38
使用子视图的概念。使用3个控件,UIButton
(200x270)、UILabel
(200x120)和UIImageView
(200x150)。将图像分配给UIImageView
并设置UILabel
的文本。根据UIButton
的(x,y)位置放置label和imageview控件。
最后,你应该拥有类似这样的东西:
对于以下实例:
UIButton *button;
UILabel *label;
UIImageView *imageView;
[button addSubView:imageView];
[button addSubView:label];
https://stackoverflow.com/questions/4344847
复制相似问题