我具有以下布局,并且我正在尝试在左侧和右侧添加填充。.
控件是禁用的UIButton。
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9jZERlcC5qcGcifQ==” alt =“在此处输入图像描述”>
我创建按钮的代码是这样:
UIButton *buttonTime = [[UIButton alloc] initWithFrame:CGRectMake(90, 10, 50, 20)];
[buttonTime setBackgroundImage:[[UIImage imageNamed:@"bubble.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:13] forState:UIControlStateDisabled];
[buttonTime setTitle:@"27 feb, 2011 11:10 PM" forState:UIControlStateDisabled];
[buttonTime setTitleColor:[UIColor blackColor] forState:UIControlStateDisabled];
buttonTime.titleLabel.font=[UIFont fontWithName:@"Helvetica" size:8.0];
buttonTime.titleLabel.lineBreakMode= UILineBreakModeWordWrap;
[buttonTime setEnabled:FALSE];
[scrollView addSubview:buttonTime];
[buttonTime release];
// Swift
var titleEdgeInsets: UIEdgeInsets!
// Objective-C
@property(nonatomic) UIEdgeInsets titleEdgeInsets;
使用此属性来调整按钮标题的有效绘图矩形的大小和位置。您可以为四个插图(上,左,下,右)中的每一个指定不同的值。正值会缩小或插入该边缘,将其移近按钮中心。负值扩展或起始于该边缘。使用UIEdgeInsetsMake函数构造此属性的值。默认值为UIEdgeInsetsZero。
https://developer.apple.com/documentation/uikit/uibutton/1624010-titleedgeinsets
您还可以通过情节提要或xib中的Interface Builder Size Inspector设置插入值。
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLmltZ3VyLmNvbS9iQm5pSHh6LnBuZyJ9” alt =“界面生成器大小检查器”>
接受的答案所面临的挑战是,设置titleEdgeInsets
是局限性,如Apple文档中所述:
此属性仅用于在布局过程中定位标题。 >按钮不使用此属性来确定internalContentSize和> sizeThatFits(_:)。
这意味着仅当按钮的大小明确适合标题标签和边距时,才设置边距。如果标题太长或页边距太大,则标题文本可能会被剪切。对于您在编译时知道其标题的按钮,这是可以的,但对于可变长度的按钮标题,可能会造成问题。
容纳可变标题长度的另一种方法是将titleEdgeInsets
保留为默认值。设置按钮标题后,添加显式的宽度和高度约束,以适应按钮的标题标签和其他边距。例如:
let margin: CGFloat = 10.0
let button = UIButton()
button.setTitle("My Button Title", for .normal)
button.widthAnchor.constraint(equalToConstant: button.titleLabel!.intrinsicContentSize.width + margin * 2.0).isActive = true
button.heightAnchor.constraint(equalToConstant: button.titleLabel!.intrinsicContentSize.height + margin * 2.0).isActive = true
放置按钮时,不添加其他高度或宽度限制,并且无论标题长度如何,按钮都将正确显示。
如果此选项不太讨厌使用UIButton子类,则此选项也是可行的
class Button: UIButton {
override var intrinsicContentSize: CGSize {
get {
let baseSize = super.intrinsicContentSize
return CGSize(width: baseSize.width + titleEdgeInsets.left + titleEdgeInsets.right,
height: baseSize.height + titleEdgeInsets.top + titleEdgeInsets.bottom)
}
}
}
然后根据需要使用titleEdgeInsets
let button = Button()
... configure button
button.titleEdgeInsets = ...
在Swift 4中,请注意使用contentEdgeInsets
而不是titleEdgeInsets
:
btn.contentEdgeInsets = UIEdgeInsetsMake(8, 8, 8, 8)
btn.titleLabel?.lineBreakMode = .byWordWrapping
这将使按钮将其文本换行并在一行中保留一行,只要有空格,并在其周围添加一些填充物
contentHorizontalAlignment
属性的作用就像一个超级按钮,如下所述:
使用上述解决方案,如果按钮周围有边框,则会删去一些文本。例如,名为“ Delete something”的按钮标签最终显示“ Dele ... ing”。如果您遇到此问题,这是解决方案:
aButton.contentEdgeInsets = UIEdgeInset.init(top: 0, left: 8, bottom: 0, right: 8)
我发现了一种简单/轻松的方法来向文本按钮添加边框(并具有左/右边界):
带有标题的创建按钮。
在情节提要中放置按钮,将其对齐在想要的位置,然后添加一个强制宽度约束,该约束是偶数(我使用20,所以它在每一侧都添加了10个点)。这将强制边框围绕您创建的宽度。
使用代码创建边框。例如:
myTextButton.backgroundColor = .clear
myTextButton.layer.cornerRadius = 5
myTextButton.layer.borderWidth = 2
myTextButton.layer.borderColor = UIColor.white.cgColor
通过编辑器(现在位于Size Inspector下)将左titleInset设置为2或通过代码。这似乎使文本居中,但是此值对于各种文本和文本大小可能会有所不同。
此文章适用于Xcode 8.1和Swift 3。