我有一個尺寸寬200和高270的按鈕。我想在同一個按鈕上有文字和圖片。不作爲該文本的背景圖片。相反,我想在同一個按鈕上顯示高度爲120和高度爲150的圖像。任何人都有想法嗎? 如果您不明白我的問題,請讓我知道。iPhone SDK:UIButton同時具有圖像和文本功能?
回答
您可以使用此代碼,它將爲你的目的
.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文件中的自定義按鈕。
使用子視圖的概念。以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];
[testBtn setBackgroudImage: [UIImage imageNamed:@"Image name.png"] forState:UIControlStateNormal];
[testBtn setTitle:@"Your text" forState:UIControlStateNormal];
在下面的代碼示出了使用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];
是的,你可以在同一個按鈕上顯示圖像和標題,只要它們都足夠小以適應。當你需要處理安置時,困難的部分來了。
您可以玩UIButton
的contentVerticalAlignment
和contentHorizontalAlignment
屬性(繼承自UIControl
)。
您還可以使用titleEdgeInsets
和imageEdgeInsets
屬性根據對齊屬性將標題和圖像從其獲取的位置移動。
如果你想要非常準確或定製的位置,我建議覆蓋UIButton
的titleRectForContentRect:
和imageRectForContentRect:
方法。這些允許您爲標題和圖像定義幀,並且只要按鈕需要佈置,就會調用它們。一個警告,如果你想參考titleRectForContentRect:
裏面的self.titleLabel
,確保首先定義self.currentTitle
。我遇到了錯誤的訪問錯誤,可能在首次初始化titleLabel時調用該方法。
如果您想更好地控制佈局,請創建UIControl
的子類,並將所需的視圖(UILabel
,UIImageView
)排列在xib文件中。然後,您可以定期使用Autolayout,而無需整理UIEdgeInsets
。
在斯威夫特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您的按鈕參考按鈕子視圖在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))
- 1. 具有圖像背景和文本及有狀態功能的Android按鈕
- 2. 打開圖像和文本文件具有相同的名稱
- 3. 按鈕具有圖像和文本
- 4. 如何使用改進功能同時發送圖像和文本
- 5. 一個網格單元格同時具有圖像和文字
- 6. Bootstrap 3面板文本和圖像(具有不同背景的圖像)
- 7. 如何將UIBarButtonItem添加到UIToolbar中同時具有文本和圖像?
- 8. Java - 具有背景圖像和正常功能的JPanel
- 9. 具有圖像處理和縮放功能的JavaScript庫
- 10. 具有UISwitch功能的iPhone UIButton
- 11. iPhone文本和圖像顯示
- 12. 提交表單同時具有動作和提交功能
- 13. 同時具有自定義功能和任務窗格
- 14. PHP:將文本更改爲具有透明度的PNG圖像的功能
- 15. 具有同步滾動和拼寫檢查功能的文本編輯器
- 16. 具有隱藏和顯示文本功能的按鈕
- 17. 具有相同的圖像
- 18. iPhone功能與圖像,點擊縮略圖和顯示大圖像
- 19. 如何創建具有圖像和文本的HTML和CSS
- 20. Javascript(EaselJS) - 敵人同時具有相同的功能?
- 21. TextView可以同時具有背景和圖像
- 22. 有沒有什麼辦法讓我的文本框和文本區域具有相同的背景圖像?
- 23. Jquery具有拖放,平移和縮放功能的單一圖像地圖
- 24. SIFT功能匹配性能,同時匹配多個圖像
- 25. Spark - Scala:解析並提取同時具有文本和圖像的文檔 - .doc,.docx文件
- 26. Android的按鈕具有不同的圖像文本的啓用和壓制
- 27. 在Php和Mysql中同時更新圖像和文本
- 28. 具有不同功能的用戶
- 29. 相同高度圖像和文本
- 30. 文本和圖像在同一行
喜阿迪亞感謝回答我的問題。我試了一下。代碼完美的工作,但一個問題是我的圖像不是固定大小。如何設置圖像大小,以便它可以適合按鈕大小? – 2010-12-08 06:16:26
這也可以直接在NIB文件中完成,它允許您預覽它的外觀,而無需在代碼中進行實驗。 – 2012-03-19 00:13:24
對於那些需要在代碼中生成按鈕的人,可以通過設置testBtn.imageEdgeInsets = UIEdgeInsetsMake(top,left,bottom,right)來調整圖像; – DonnaLea 2012-05-23 23:20:54