【问题标题】:UILabel text margin [duplicate]UILabel 文本边距 [重复]
【发布时间】:2011-03-29 10:41:42
【问题描述】:

我正在寻找设置UILabel 的左插图/边距,但找不到这样做的方法。标签具有背景设置,因此仅更改其原点不会起作用。最好在左侧插入10px 左右的文本。

【问题讨论】:

  • 一旦你子类化,对于插图来说,它就是stackoverflow.com/a/43197662/294884
  • 另一种方法可能是将您的标签嵌入到水平堆栈视图中,并在您希望的任何宽度的左侧/右侧添加 uiview。
  • 我们终于,终于彻底解决了这个问题。你必须在 textRect 中调用 super LAST:stackoverflow.com/a/58876988/294884

标签: ios cocoa-touch uikit uilabel


【解决方案1】:

向 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;
}

- (void)drawTextInRect:(CGRect)rect {
    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.edgeInsets)];
}

- (CGSize)intrinsicContentSize
{
    CGSize size = [super intrinsicContentSize];
    size.width  += self.edgeInsets.left + self.edgeInsets.right;
    size.height += self.edgeInsets.top + self.edgeInsets.bottom;
    return size;
}

@end

【讨论】:

  • 或者使用 TTTAttributedLabel (github.com/mattt/TTTAttributedLabel)
  • 这个解决方案有一个问题 - 如果文本足够长并且插入足够大,标签文本的最后一行将被切断。刚刚尝试使用最新的 iOS 7。
  • 您还应该覆盖 intrinsicContentSize 以增加内在大小以包含插图,以便自动布局正常工作。
  • 如果我设置numberOfLines = 0,它会截断我的文本:(
  • @AsifBilal 您还需要覆盖 textRectForBounds: 方法。
【解决方案2】:

为了摆脱单行标签的垂直填充,我做了:

// I have a category method setFrameHeight; you'll likely need to modify the frame.
[label setFrameHeight:font.pointSize];

或者,没有类别,使用:

CGRect frame = label.frame;
frame.size.height = font.pointSize;
label.frame = frame;

【讨论】:

    【解决方案3】:

    也许用https://github.com/mattt/TTTAttributedLabel代替UILabel

    BITAttributedLabel *label = [BITAttributedLabel new];
    label.font = font;
    label.text = @"hello";
    label.textInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    [label sizeToFit];
    

    【讨论】:

      【解决方案4】:

      如果您在 iOS 6+ 中使用自动布局,您可以通过调整 UILabel 的子类中的 intrinsicContentSize 来做到这一点。

      - (id)initWithFrame:(CGRect)frame
      {
          self = [super initWithFrame:frame];
          if (self) {
              self.textAlignment = NSTextAlignmentRight;
          }
          return self;
      }
      
      - (CGSize)intrinsicContentSize 
      {
          CGSize size = [super intrinsicContentSize];
          return CGSizeMake(size.width + 10.0, size.height);
      }
      

      【讨论】:

        【解决方案5】:

        您也可以通过使用自定义框架初始化 UILabel 来解决此问题。

            CGRect initialFrame = CGRectMake(0, 0, 100, 100);
            UIEdgeInsets contentInsets = UIEdgeInsetsMake(0, 10, 0, 0);
            CGRect paddedFrame = UIEdgeInsetsInsetRect(initialFrame, contentInsets);
        
            self.label = [[UILabel alloc] initWithFrame:paddedFrame];
        

        CGRect Tricks点头。

        【讨论】:

        • 是的,但如果标签有背景,那就没用了
        • 初始帧在自动布局中几乎被忽略了。
        【解决方案6】:

        将标签的textAlignment 属性设置为NSTextAlignmentRight 并增加其宽度。

        【讨论】:

          【解决方案7】:

          我认为UILabel 类没有设置边距的方法。为什么不将 Label 的位置设置在需要的位置?

          见以下代码:

          UILabel *label = [[UILabel alloc] init];
          label.text = @"This is label";
          label.frame = CGRectMake(0,0,100,100);
          

          如果来自界面生成器,则只需通过以下方式定位标签:

          yourLabel.frame = CGRectMake(0,0,100,100);
          

          【讨论】:

            【解决方案8】:

            如果你不想使用额外的父视图来设置背景,你可以继承 UILabel 并覆盖 textRectForBounds:limitedToNumberOfLines:。我会添加一个 textEdgeInsets 属性或类似的,然后做

            - (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
            {
              return [super textRectForBounds:UIEdgeInsetsInsetRect(bounds,textEdgeInsets) limitedToNumberOfLines:numberOfLines];
            }
            

            为了健壮性,您可能还想在 setTextEdgeInsets: 中调用 [self setNeedsDisplay],但我通常不会打扰。

            【讨论】:

            • 请注意,从文档中,“要调用此方法,必须事先调用 sizeToFit 或 sizeThatFits: 方法。”
            • @mvds:没关系:textRectForBounds:limitedToNumberOfLines: 正在被调用,所以由调用它的人来确保之前调用过 -sizeToFit/-sizeThatFits:
            • 这似乎不适用于 Xcode 9.3/Swift 4.1/iOS 11.3。 textRectForBounds() 被调用并且我的边缘插图存在,但标签没有任何这种填充。我看到宽度和高度的奇怪值。这是来自textRectForBounds()bounds 的描述:Printing description of bounds: ▿ (0.0, 0.0, 3.40282346638529e+38, 3.40282346638529e+38)。标签正在视图控制器中的viewDidLoad() 中创建。
            • 将此代码添加到 Brody Robertson 的答案终于对我有用。如果不覆盖 textRectForBounds:limitedToNumberOfLines: 我的字符串的最后一行(从 html 转换的多行属性字符串)将被截断。我正在使用 Xcode 10.0 和 iOS 12。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-03-26
            • 2011-11-25
            • 1970-01-01
            • 2016-01-24
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多