【问题标题】:How to achieve UIButton / UILabel 'padding' in iPhone app如何在 iPhone 应用程序中实现 UIButton / UILabel 'padding'
【发布时间】:2011-03-04 10:08:56
【问题描述】:

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

这可能是一个非常愚蠢的问题,但是如何应用“填充”将文本移出左侧边缘?

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

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

非常感谢。

【问题讨论】:

  • Marco Arment 在推特上发布了一个非常简单的 UILabel 子类,它在标签绘图中添加了 UIEdgeInsets 样式的填充:它在 gist.github 上作为 IPInsetLabel

标签: iphone uiview uibutton uilabel padding


【解决方案1】:

好的,到目前为止我发现的最简单的解决方案是:

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

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

【讨论】:

  • 我未能调整此解决方案以实现左对齐。这是一个非常古老的答案,我想它可能不适用于 iOS 6。希望有人可以提供更新。
  • 效果很好,但是从 iOS 6.0 开始你必须使用 NSTextAlignmentCenter,UITextAlignmentCenter 已被弃用。
【解决方案2】:

我正在尝试实现类似的事情,即“填充”UILabel。我一直在尝试实施托比在上面发布的解决方案,但似乎找不到需要去哪里。 我正在使用的 UILabel 左对齐 - 这是导致问题的原因吗?

我已经尝试在viewDidLoad 中使用它,甚至在方法中的 UILabel 的子类中使用它:

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

【讨论】:

    【解决方案3】:

    要向 UILabel 添加填充,最灵活的方法是继承 UILabel 并添加一个 edgeInsets 属性。然后设置所需的插图,标签将被相应地绘制。

    OSLabel.h

    #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
    

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

        这是 UILabel 的一个子类,它使用 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
        

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

        【讨论】:

          【解决方案6】:

          如果您只是在一行上寻找水平填充,那么这可能就足够了(对我来说):

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

          【讨论】:

            【解决方案7】:

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

            ObjC

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

            斯威夫特

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

            【讨论】:

            • 请注意,对于未来的读者,contentEdgeInsets 在 iOS 15 中似乎已弃用。现在,当使用 UIButtonConfiguration 时,该属性将被忽略。其相关方法.setDefaultContentInsets正在以Beta Software通知的形式呈现。
            【解决方案8】:

            要在UIButton 文本中设置填充,您需要使用UIButtoncontentEdgeInsets 属性。

            在 Storyboard 中,设置内容插入执行以下步骤:

            1. 选择视图控制器,然后选择UIButton
            2. 转到实用程序面板(Command + Option + 0)并选择属性检查器(Command + Option + 4)
            3. 现在选择 Edge 中的 Content 并根据您的设置 Inset 要求(见截图)

            【讨论】:

            • 现在它的尺寸检查器
            • 现在是按钮 -> 大小检查器下的内容插入
            • XCode 9 及以上 UIButton -> Size Inspector -> Content Insets ==> 如下图所示:stackoverflow.com/a/49959281
            【解决方案9】:

            Xcode 9 上,插图现在位于 Size Inspector 而不是 Attributes Inspector:

            【讨论】:

              【解决方案10】:

              如何创建一个扩展 UIButton 的自定义类并覆盖它:

              - (CGRect)titleRectForContentRect:(CGRect)contentRect
              {
                  return UIEdgeInsetsInsetRect(contentRect, UIEdgeInsetsMake(topPadding, rightPadding, bottomPadding, leftPadding));
              }
              

              在 UILabel 的情况下,只需覆盖:

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

              【讨论】:

                猜你喜欢
                • 2011-07-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-10-15
                相关资源
                最近更新 更多