如何在iPhone应用中实现UIButton / UILabel“padding”

时间:2022-07-25 21:09:12

I've got various views in my iPhone application that require padding e.g a custom UIButton with text aligned left, and a UILabel with a background color.

我的iPhone应用程序中有各种需要填充e的视图。一个自定义UIButton与文本对齐左边,和一个UILabel与一个背景颜色。

This may be a really stupid question, but how can I apply 'padding' to move the text off the left hand edge?

这可能是一个非常愚蠢的问题,但是如何使用“填充”将文本从左手边移开呢?

I've tired using bounds etc without any success.

我已经厌倦了使用边界等等,但没有任何成功。

I know I could create a wrapper view for my UILabel with a background color, but it seems like overkill.

我知道我可以用背景颜色为UILabel创建一个包装器视图,但这似乎有点过头了。

Many thanks.

多谢。

10 个解决方案

#1


76  

I am using auto layout. Solution that worked for me was setting UIButton's contentEdgeInsets.

我正在使用自动布局。对我有用的解决方案是设置UIButton的contentEdgeInsets。

ObjC

button.contentEdgeInsets = UIEdgeInsetsMake(0.0f, 30.0f, 0.0f, 30.0f);

Swift

button.contentEdgeInsets = UIEdgeInsets(top: 0.0, left: 30.0, bottom: 0.0, right: 30.0)

#2


36  

Here Dave explains how to add padding to a UIButton title, hope this helps. He uses the titleEdgeInsets property on UIButton.

在这里,Dave解释了如何向UIButton标题添加填充,希望这能有所帮助。他在UIButton上使用titleEdgeInsets属性。

#3


23  

Ok the simplest solution I've found so far is:

到目前为止我找到的最简单的解是

self.textAlignment = UITextAlignmentCenter; 
[self sizeToFit]; 
CGRect frame = self.frame;
frame.size.width += 20; //l + r padding 
self.frame = frame;

Not exactly what I wanted, but it works.

不完全是我想要的,但它确实有效。

#4


11  

To set padding in uibutton text you need to use UIButton's contentEdgeInsets property. In Storyboard,to set content inset do following steps:-

要在uibutton文本中设置填充,需要使用uibutton的contentEdgeInsets属性。在故事板中,要设置内容,请执行以下步骤:-

  1. Select View controller,select uibutton
  2. 选择视图控制器,选择uibutton
  3. Go to Utilities Panel(Command + Option + 0) and select Attributes Inspector(Command + Option + 4)
  4. 转到实用工具面板(命令+选项+ 0)并选择属性检查器(命令+选项+ 4)
  5. Now select Content in Edge and set Inset as per your requirement.(see screenshot)
  6. 现在,根据您的需求在边缘中选择内容并设置Inset。(见截图)

如何在iPhone应用中实现UIButton / UILabel“padding”

#5


6  

To add padding to UILabel the most flexible approach is to subclass UILabel and add an edgeInsets property. You then set the desired insets and the label will be drawn accordingly.

要向UILabel添加填充,最灵活的方法是子类化UILabel并添加一个edgeInsets属性。然后设置所需的嵌套,并相应地绘制标签。

OSLabel.h

OSLabel.h

#import <UIKit/UIKit.h>

@interface OSLabel : UILabel

@property (nonatomic, assign) UIEdgeInsets edgeInsets;

@end

OSLabel.m

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

#6


2  

CGRectInset is your friend. You can set negative 'insets' to create padding:

CGRectInset是你的朋友。您可以设置负的“insets”来创建填充:

[self sizeToFit];
CGRect rect = self.frame;
rect = CGRectInset( rect, -6.f, -5.f); // h inset, v inset
self.frame = rect;

#7


2  

Here's a sublass of UILabel that has customizable padding using an edgeInset property:

这里有一个UILabel的sublass,它使用edgeInset属性可定制填充:

PaddedLabel.h

PaddedLabel.h

#import <UIKit/UIKit.h>
@interface PaddedLabel : UILabel
@property UIEdgeInsets edgeInsets;
@end

PaddedLabel.m

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

(This is a simplification of Brody's answer which also works with autolayout.)

(这是对Brody的回答的简化,也适用于自动布局。)

#8


0  

I'm trying to achieve a similar thing, that is 'pad' a UILabel. I've been trying to implement the solution that Toby posted above, but can't seem to find where this needs to go. The UILabel I'm working with is aligned left - is that what's causing the issue?

我正在尝试实现一个类似的东西,那就是“pad”a UILabel。我一直在尝试实现托比在上面发布的解决方案,但似乎找不到这需要去哪里。与我合作的UILabel是左对齐的——这就是问题的原因吗?

I've tried using this in viewDidLoad, and even in a subclass of UILabel in the method:

我尝试过在viewDidLoad中使用这个,甚至在UILabel的一个子类中使用这个方法:

- (CGRect)textRectForBounds:(CGRect)bounds
     limitedToNumberOfLines:(NSInteger)numberOfLines 

#9


0  

If you're just looking for a horizontal padding on one line, then this may be enough (it was for me):

如果你只是在一条直线上寻找一个水平的填充,那么这可能就足够了(对我来说就是这样):

NSString* padding = @"  "; // 2 spaces
myLabel.text = [NSString stringWithFormat:@"%@%@%@", padding, name, padding];

#10


0  

On Xcode 9, the insets are now located in the Size Inspector instead of the Attributes Inspector:

在Xcode 9中,insets现在位于Size Inspector而不是Attributes Inspector:

如何在iPhone应用中实现UIButton / UILabel“padding”

#1


76  

I am using auto layout. Solution that worked for me was setting UIButton's contentEdgeInsets.

我正在使用自动布局。对我有用的解决方案是设置UIButton的contentEdgeInsets。

ObjC

button.contentEdgeInsets = UIEdgeInsetsMake(0.0f, 30.0f, 0.0f, 30.0f);

Swift

button.contentEdgeInsets = UIEdgeInsets(top: 0.0, left: 30.0, bottom: 0.0, right: 30.0)

#2


36  

Here Dave explains how to add padding to a UIButton title, hope this helps. He uses the titleEdgeInsets property on UIButton.

在这里,Dave解释了如何向UIButton标题添加填充,希望这能有所帮助。他在UIButton上使用titleEdgeInsets属性。

#3


23  

Ok the simplest solution I've found so far is:

到目前为止我找到的最简单的解是

self.textAlignment = UITextAlignmentCenter; 
[self sizeToFit]; 
CGRect frame = self.frame;
frame.size.width += 20; //l + r padding 
self.frame = frame;

Not exactly what I wanted, but it works.

不完全是我想要的,但它确实有效。

#4


11  

To set padding in uibutton text you need to use UIButton's contentEdgeInsets property. In Storyboard,to set content inset do following steps:-

要在uibutton文本中设置填充,需要使用uibutton的contentEdgeInsets属性。在故事板中,要设置内容,请执行以下步骤:-

  1. Select View controller,select uibutton
  2. 选择视图控制器,选择uibutton
  3. Go to Utilities Panel(Command + Option + 0) and select Attributes Inspector(Command + Option + 4)
  4. 转到实用工具面板(命令+选项+ 0)并选择属性检查器(命令+选项+ 4)
  5. Now select Content in Edge and set Inset as per your requirement.(see screenshot)
  6. 现在,根据您的需求在边缘中选择内容并设置Inset。(见截图)

如何在iPhone应用中实现UIButton / UILabel“padding”

#5


6  

To add padding to UILabel the most flexible approach is to subclass UILabel and add an edgeInsets property. You then set the desired insets and the label will be drawn accordingly.

要向UILabel添加填充,最灵活的方法是子类化UILabel并添加一个edgeInsets属性。然后设置所需的嵌套,并相应地绘制标签。

OSLabel.h

OSLabel.h

#import <UIKit/UIKit.h>

@interface OSLabel : UILabel

@property (nonatomic, assign) UIEdgeInsets edgeInsets;

@end

OSLabel.m

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

#6


2  

CGRectInset is your friend. You can set negative 'insets' to create padding:

CGRectInset是你的朋友。您可以设置负的“insets”来创建填充:

[self sizeToFit];
CGRect rect = self.frame;
rect = CGRectInset( rect, -6.f, -5.f); // h inset, v inset
self.frame = rect;

#7


2  

Here's a sublass of UILabel that has customizable padding using an edgeInset property:

这里有一个UILabel的sublass,它使用edgeInset属性可定制填充:

PaddedLabel.h

PaddedLabel.h

#import <UIKit/UIKit.h>
@interface PaddedLabel : UILabel
@property UIEdgeInsets edgeInsets;
@end

PaddedLabel.m

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

(This is a simplification of Brody's answer which also works with autolayout.)

(这是对Brody的回答的简化,也适用于自动布局。)

#8


0  

I'm trying to achieve a similar thing, that is 'pad' a UILabel. I've been trying to implement the solution that Toby posted above, but can't seem to find where this needs to go. The UILabel I'm working with is aligned left - is that what's causing the issue?

我正在尝试实现一个类似的东西,那就是“pad”a UILabel。我一直在尝试实现托比在上面发布的解决方案,但似乎找不到这需要去哪里。与我合作的UILabel是左对齐的——这就是问题的原因吗?

I've tried using this in viewDidLoad, and even in a subclass of UILabel in the method:

我尝试过在viewDidLoad中使用这个,甚至在UILabel的一个子类中使用这个方法:

- (CGRect)textRectForBounds:(CGRect)bounds
     limitedToNumberOfLines:(NSInteger)numberOfLines 

#9


0  

If you're just looking for a horizontal padding on one line, then this may be enough (it was for me):

如果你只是在一条直线上寻找一个水平的填充,那么这可能就足够了(对我来说就是这样):

NSString* padding = @"  "; // 2 spaces
myLabel.text = [NSString stringWithFormat:@"%@%@%@", padding, name, padding];

#10


0  

On Xcode 9, the insets are now located in the Size Inspector instead of the Attributes Inspector:

在Xcode 9中,insets现在位于Size Inspector而不是Attributes Inspector:

如何在iPhone应用中实现UIButton / UILabel“padding”