可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am able to set the image and text for button. But it is aligned horizontally. I want the image and text to be aligned vertically inside the button. i.e., text below the image
How to make these changes using storyboard?
What I want is this:
What I am getting now is this:
回答1:
Yes you can do it from storyboard without writing any logic also.
1) Select button and go to Attribute Inspector
in your storyboard.
2) Assign Image to the button. (Don't use background Image)
3) Set Title text to that button.
4) Now you need to set edge
and Inset
so first select image from edge and set Inset as you need and then select title from edge and set inset as per your need.
Hope this helps.
回答2:
If you are looking for output like this
1) Select button and go to Attribute Inspector in your storyboard to get this result of Button size 50*50
2) Now set Title Insets of Button
回答3:
I've written an extension that do the works for you, Swift 4 compatible.
public extension UIButton {
func alignTextBelow(spacing: CGFloat = 6.0) {
if let image = self.imageView?.image {
let imageSize: CGSize = image.size
self.titleEdgeInsets = UIEdgeInsetsMake(spacing, -imageSize.width, -(imageSize.height), 0.0)
let labelString = NSString(string: self.titleLabel!.text!)
let titleSize = labelString.size(withAttributes: [NSAttributedStringKey.font: self.titleLabel!.font])
self.imageEdgeInsets = UIEdgeInsetsMake(-(titleSize.height + spacing), 0.0, 0.0, -titleSize.width)
}
}
}
Where spacing
is the distance between image and text.
回答4:
You can easily and visually achieve the alignment using the Edge
property of the Button from Property Window(Attribute Inspector) you can change the inset
values of Title
, and Image
as per your need, see image below
Hope it helps.
Cheers.
回答5:
Swift 4.2 compatible code is here , you just need to call this function
public extension UIButton
{
func alignTextUnderImage(spacing: CGFloat = 6.0)
{
if let image = self.imageView?.image
{
let imageSize: CGSize = image.size
self.titleEdgeInsets = UIEdgeInsets(top: spacing, left: -imageSize.width, bottom: -(imageSize.height), right: 0.0)
let labelString = NSString(string: self.titleLabel!.text!)
let titleSize = labelString.size(withAttributes: [NSAttributedString.Key.font: self.titleLabel!.font])
self.imageEdgeInsets = UIEdgeInsets(top: -(titleSize.height + spacing), left: 0.0, bottom: 0.0, right: -titleSize.width)
}
}
}
回答6:
a refined one
func alignTextUnderImage(spacing: CGFloat = 6.0) {
guard let image = imageView?.image, let label = titleLabel,
let string = label.text else { return }
titleEdgeInsets = UIEdgeInsets(top: spacing, left: -image.size.width, bottom: -image.size.height, right: 0.0)
let titleSize = string.size(withAttributes: [NSAttributedString.Key.font: label.font])
imageEdgeInsets = UIEdgeInsets(top: -(titleSize.height + spacing), left: 0.0, bottom: 0.0, right: -titleSize.width)
}
回答7:
Thank you @rbiard. Another workaround when you are developing a multilingual mobile app and when using UISemanticContentAttribute (right to left and left to right). This should work:
func alignTextBelow(spacing: CGFloat = 10.0) {
//when the selected language is English
if L102Language.currentAppleLanguage() == "en" {
guard let image = imageView?.image, let label = titleLabel,
let string = label.text else { return }
titleEdgeInsets = UIEdgeInsets(top: spacing, left: -image.size.width, bottom: -image.size.height, right: 0.0)
let titleSize = string.size(withAttributes: [NSAttributedString.Key.font: label.font])
imageEdgeInsets = UIEdgeInsets(top: -(titleSize.height + spacing), left: 0.0, bottom: 0.0, right: -titleSize.width)
} else {
//When selecting a right to left language. For example, Arabic
guard let image = imageView?.image, let label = titleLabel,
let string = label.text else { return }
titleEdgeInsets = UIEdgeInsets(top: spacing, left: 0, bottom: -image.size.height, right: -image.size.width)
let titleSize = string.size(withAttributes: [NSAttributedString.Key.font: label.font])
imageEdgeInsets = UIEdgeInsets(top: -(titleSize.height + spacing), left: -titleSize.width, bottom: 0.0, right: 0)
}
}
回答8:
You cannot change layout directly in UIButton, but you can try to use these properties on UIButton:
UIButton *button = ...
button.titleEdgeInsets = UIEdgeInsetsMake(....);
button.imageEdgeInsets = UIEdgeInsetsMake(....);
If it won't help I'd recommend to subclass from UIResponder and make your own component and do your own layout as you needed it.