我有一个宽度为200,高度为270的按钮。我想在同一按钮上显示文本和图像。不作为该文本的背景图像。取而代之的是,我想在同一按钮上显示高度为120的文本和高度为150的图像。怎么做?
Answers:
您可以使用此代码,它将达到您的目的
.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];
根据需要调整按钮的坐标和大小。这是指导您的示例代码。
PS:在笔尖文件中添加一个UIButton>将其设为自定义按钮>将IBOutlet连接到笔尖文件中的自定义按钮即可。
下面的代码显示使用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];
是的,只要它们都足够小,就可以在同一按钮中显示图像和标题。当您需要处理展示位置时,困难的部分就来了。
您可以使用(继承自)的contentVerticalAlignment
和contentHorizontalAlignment
属性。UIButton
UIControl
您还可以使用titleEdgeInsets
和imageEdgeInsets
属性,根据对齐属性将标题和图像从它们获得的位置移开。
如果您想要非常准确或自定义的展示位置,建议您覆盖的titleRectForContentRect:
和imageRectForContentRect:
方法UIButton
。这些允许您定义标题和图像的框架,并在需要布置按钮时调用它们。一个警告,如果你想引用self.titleLabel
里面titleRectForContentRect:
,一定self.currentTitle
首先被定义。我遇到了严重的访问错误,可能是在首次初始化titleLabel时调用了该方法。
使用子视图的概念。UIButton
选取3个控制项(200x270),UILabel
(200x120)和UIImageView
(200x150)。将图片分配给,UIImageView
然后为设置文字UILabel
。根据的(x,y)位置放置标签和imageview控件UIButton
。
最后,您应该具有以下内容:
对于以下情况:
UIButton *button;
UILabel *label;
UIImageView *imageView;
[button addSubView:imageView];
[button addSubView:label];
在Swift 3中
let button = UIButton()
//make sure the image (jpg, png) you are placing in the button
//is about the right size or it will push the label off the button
//add image
let image = UIImage(named:"my_button_image")
button.setImage(image, for: .normal)
button.imageView?.contentMode = .scaleAspectFit
button.imageEdgeInsets = UIEdgeInsets(top:0, left:-30, bottom:0, right:0) //adjust these to have fit right
//add title
button.setTitle("Fan", for: .normal)
button.titleEdgeInsets = UIEdgeInsets(top:0, left:10, bottom:0, right:0) //adjust insets to have fit how you want
//add button to your view - like in viewDidLoad or your own custom view setup
addSubview(button)
如果使用情节提要,以编程方式设置锚以使其显示/附加到Interface Builder中的按钮参考上,请不要忘记设置锚
[testBtn setBackgroudImage: [UIImage imageNamed:@"Image name.png"] forState:UIControlStateNormal];
[testBtn setTitle:@"Your text" forState:UIControlStateNormal];
在Swift 4中,使用按钮子视图对我有用。
将标签限制在按钮内
let imageAndTextButton : UIButton = {
let button = UIButton(frame: .zero)
button.tintColor = .clear
button.setImage(#imageLiteral(resourceName: "buttonImage"), for: .normal)
button.imageView?.contentMode = .scaleToFill
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
let textForButtonLabel = UILabel(frame: .zero)
textForButtonLabel.translatesAutoresizingMaskIntoConstraints = false
textForButtonLabel.attributedText = NSAttributedString(string: "Text For Button", attributes: buttonTextAttributes)
textForButtonLabel.textAlignment = .center
textForButtonLabel.backgroundColor = .clear
imageAndTextButton.addSubview(textForButtonLabel)
imageAndTextButton.addConstraint(NSLayoutConstraint(item: textForButtonLabel, attribute: .centerX, relatedBy: .equal, toItem: imageAndTextButton, attribute: .centerX, multiplier: 1.0, constant: 0))
imageAndTextButton.addConstraint(NSLayoutConstraint(item: textForButtonLabel, attribute: .centerY, relatedBy: .equal, toItem: imageAndTextButton, attribute: .centerY, multiplier: 1.0, constant: 0))
试试这个对我有用
我们必须根据按钮的大小和要求设置UIEdgeInsets的值。
[self.testButton setTitle: @"myTitle" forState: UIControlStateNormal];
UIImage *iconImage = [UIImage imageWithIcon:@"fa-dot-circle-o" backgroundColor:[UIColor clearColor] iconColor:[UIColor whiteColor] fontSize:10.0f];
//UIEdgeInsets insets = {top, left, bottom, right};
[self.testButton setImageEdgeInsets:UIEdgeInsetsMake(3.0, 0.0, 3.0, 2.0)];
[self.testButton setTitleEdgeInsets:UIEdgeInsetsMake(3.0, 1.0, 3.0, 0.0)];
[self.testButton setImage:iconImage forState:UIControlStateNormal];
[self.testButton addTarget:self action:@selector(testButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.testButton];
希望这会有所帮助。