我在iPhone应用程序中有各种视图需要填充,例如左对齐的自定义UIButton和具有背景颜色的UILabel。
这可能是一个非常愚蠢的问题,但我该如何应用“填充”来将文本移离左边缘?
我尝试使用bounds等方法都没有成功。
我知道我可以为我的UILabel
创建一个具有背景颜色的包装视图,但这似乎太过繁琐。
非常感谢。
button.configuration.contentInsets = NSDirectionalEdgeInsetsMake(0.0f, 30.0f, 0.0f, 30.0f);
button.configuration.contentInsets = NSDirectionalEdgeInsets(top: 0.0, left: 30.0, bottom: 0.0, right: 30.0)
contentEdgeInsets
似乎在 iOS 15 中已被弃用。现在当使用 UIButtonConfiguration 时,该属性将被忽略。与此相关的方法 .setDefaultContentInsets 正在搭配 Beta Software
通知一起发布。 - ufukty好的,到目前为止我找到的最简单的解决方案是:
self.textAlignment = UITextAlignmentCenter;
[self sizeToFit];
CGRect frame = self.frame;
frame.size.width += 20; //l + r padding
self.frame = frame;
不完全是我想要的,但它可以工作。
Size Inspector
中有一个Content Insets
选项,如此图所示:https://dev59.com/z3A75IYBdhLWcg3w4tR7#49959281 - Bruno Bieri#import <UIKit/UIKit.h>
@interface OSLabel : UILabel
@property (nonatomic, assign) UIEdgeInsets edgeInsets;
@end
OSLabel.m
#import "OSLabel.h"
@implementation OSLabel
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.edgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if(self){
self.edgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
}
return self;
}
- (void)drawTextInRect:(CGRect)rect {
return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.edgeInsets)];
}
@end
以下是一个UILabel的子类,它拥有可自定义的padding,使用edgeInset属性:
PaddedLabel.h
#import <UIKit/UIKit.h>
@interface PaddedLabel : UILabel
@property UIEdgeInsets edgeInsets;
@end
PaddedLabel.m
#import "PaddedLabel.h"
@implementation PaddedLabel
-(void)drawTextInRect:(CGRect)rect {
return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.edgeInsets)];
}
-(CGSize)intrinsicContentSize {
CGSize contentSize = [super intrinsicContentSize];
UIEdgeInsets insets = self.edgeInsets;
contentSize.height += insets.top + insets.bottom;
contentSize.width += insets.left + insets.right;
return contentSize;
}
@end
CGRectInset 是您的好朋友。您可以设置负“插入”来创建填充:
[self sizeToFit];
CGRect rect = self.frame;
rect = CGRectInset( rect, -6.f, -5.f); // h inset, v inset
self.frame = rect;
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
return UIEdgeInsetsInsetRect(contentRect, UIEdgeInsetsMake(topPadding, rightPadding, bottomPadding, leftPadding));
}
- (CGRect)textRectForBounds:(CGRect)bounds
limitedToNumberOfLines:(NSInteger)numberOfLines;
我正在尝试实现类似的功能,即“填充”UILabel
。我一直在尝试实现Toby上面发布的解决方案,但似乎找不到需要放置在哪里。
我正在使用的UILabel是左对齐的 - 这是导致问题的原因吗?
我已经尝试在viewDidLoad
中使用它,甚至在UILabel的子类中使用该方法:
- (CGRect)textRectForBounds:(CGRect)bounds
limitedToNumberOfLines:(NSInteger)numberOfLines
按钮
和按钮.titleLabel
之间激活NSLayoutConstraint
。let button = UIButton()
button.setTitle("Retake", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(button)
guard let label = button.titleLabel else { return }
NSLayoutConstraint.activate([
// setup constraints for button relative to view
label.leadingAnchor.constraint(equalTo: button.leadingAnchor, constant: 10),
label.centerXAnchor.constraint(equalTo: button.centerXAnchor)
])
UILabel
子类,它在标签绘制中添加了UIEdgeInsets
样式的填充:它在gist.github上作为IPInsetLabel。 - cbowns