ios 故事板在按钮内的图像下方定位文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37915212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
storyboard positioning text below image inside a button
提问by Ahalya
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:
我想要的是这个:
采纳答案by Jigar Tarsariya
Yes you can do it from storyboard without writing any logic also.
是的,您也可以从情节提要中完成,而无需编写任何逻辑。
1) Select button and go to Attribute Inspector
in your storyboard.
1) 选择按钮并转到Attribute Inspector
您的故事板。
2) Assign Image to the button. (Don't use background Image)
2) 将图像分配给按钮。(不要使用背景图片)
3) Set Title text to that button.
3) 将标题文本设置为该按钮。
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.
4)现在您需要设置edge
,Inset
因此首先从边缘选择图像并根据需要设置插入,然后从边缘选择标题并根据需要设置插入。
Hope this helps.
希望这可以帮助。
回答by Sourabh Sharma
回答by Luca Davanzo
I've written an extension that do the works for you, Swift 4compatible.
我已经编写了一个可以为您工作的扩展,与Swift 4兼容。
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.
spacing
图片和文字的距离在哪里。
回答by iphonic
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
您可以使用属性窗口(属性检查器)中Edge
Button的属性轻松直观地实现对齐,您可以更改 的值,并根据需要,请参见下图inset
Title
Image
Hope it helps.
希望能帮助到你。
Cheers.
干杯。
回答by Shahzaib Maqbool
Swift 4.2 compatible code is here , you just need to call this function
Swift 4.2 兼容代码在这里,你只需要调用这个函数
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)
}
}
}
回答by rbiard
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)
}
回答by Maryoomi1
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:
谢谢@rbiard。开发多语言移动应用程序和使用 UISemanticContentAttribute(从右到左和从左到右)时的另一种解决方法。这应该有效:
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)
}
}
回答by Konstantin
You cannot change layout directly in UIButton, but you can try to use these properties on UIButton:
您不能直接在 UIButton 中更改布局,但您可以尝试在 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.
如果它没有帮助,我建议从 UIResponder 子类化并制作自己的组件并根据需要进行自己的布局。