2010-12-03 26 views

回答

109

您可以使用此代碼,它將爲你的目的

.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:在nib文件中取一個UIButton>使其成爲一個自定義按鈕>將IBOutlet連接到nib文件中的自定義按鈕。

+0

喜阿迪亞感謝回答我的問題。我試了一下。代碼完美的工作,但一個問題是我的圖像不是固定大小。如何設置圖像大小,以便它可以適合按鈕大小? – 2010-12-08 06:16:26

+1

這也可以直接在NIB文件中完成,它允許您預覽它的外觀,而無需在代碼中進行實驗。 – 2012-03-19 00:13:24

+3

對於那些需要在代碼中生成按鈕的人,可以通過設置testBtn.imageEdgeInsets = UIEdgeInsetsMake(top,left,bottom,right)來調整圖像; – DonnaLea 2012-05-23 23:20:54

5

使用子視圖的概念。以3個控件,UIButton(200x270),UILabel(200x120)和UIImageView(200x150)。將圖像指定給UIImageView並設置UILabel的文字。根據UIButton的(x,y)位置定位標籤和imageview控件。

最後,你應該有這樣的:

對於以下情況:

UIButton *button; 
UILabel *label; 
UIImageView *imageView; 

[button addSubView:imageView]; 
[button addSubView:label]; 
5
[testBtn setBackgroudImage: [UIImage imageNamed:@"Image name.png"] forState:UIControlStateNormal]; 
[testBtn setTitle:@"Your text" forState:UIControlStateNormal]; 
14

在下面的代碼示出了使用setImageEdgeInsets和setTitleEdgeInsets UIButton的類的屬性將圖像和文本中的按鈕。

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]; 
4

是的,你可以在同一個按鈕上顯示圖像和標題,只要它們都足夠小以適應。當你需要處理安置時,困難的部分來了。

您可以玩UIButtoncontentVerticalAlignmentcontentHorizontalAlignment屬性(繼承自UIControl)。

您還可以使用titleEdgeInsetsimageEdgeInsets屬性根據對齊屬性將標題和圖像從其獲取的位置移動。

如果你想要非常準確或定製的位置,我建議覆蓋UIButtontitleRectForContentRect:imageRectForContentRect:方法。這些允許您爲標題和圖像定義幀,並且只要按鈕需要佈置,就會調用它們。一個警告,如果你想參考titleRectForContentRect:裏面的self.titleLabel,確保首先定義self.currentTitle。我遇到了錯誤的訪問錯誤,可能在首次初始化titleLabel時調用該方法。

1

如果您想更好地控制佈局,請創建UIControl的子類,並將所需的視圖(UILabel,UIImageView)排列在xib文件中。然後,您可以定期使用Autolayout,而無需整理UIEdgeInsets

2

在斯威夫特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) 

不要忘記設置錨,如果這樣做編程它是否出現/如果使用的是故事板

0

使用它連接到在Interface Builder您的按鈕參考按鈕子視圖在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)) 
    
相關問題