欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UILabel文字边距

程序员文章站 2024-01-15 17:51:52
...

本文翻译自:UILabel text margin

I'm looking to set the left inset/margin of a UILabel and can't find a method to do so. 我想设置UILabel的左边插入/边距,但找不到这样做的方法。 The label has a background set so just changing its origin won't do the trick. 标签有一个背景设置,所以只是改变它的起源不会有效。 It would be ideal to inset the text by 10px or so on the left hand side. 将文本插入左侧10px左右是理想的。


#1楼

参考:https://stackoom.com/question/EaQw/UILabel文字边距


#2楼

I ended up just adding some spaces to the text: 我最后只是在文本中添加了一些空格:

self.titleLabel.text = [NSString stringWithFormat:@"    %@", self.titleLabel.text];

Ugly yet effective, and no subclassing required. 丑陋但有效,不需要子类化。

You can try "\\t" as well. 你也可以尝试“\\ t”。 For a generic solution please refer to the accepted answer 对于通用解决方案,请参阅接受的答案


#3楼

The best approach to add padding to a UILabel is to subclass UILabel and add an edgeInsets property. 向UILabel添加填充的最佳方法是继承UILabel并添加edgeInsets属性。 You then set the desired insets and the label will be drawn accordingly. 然后,您可以设置所需的插图,并相应地绘制标签。

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

- (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

#4楼

You can also solve this by initializing your UILabel with a custom frame. 您也可以通过使用自定义框架初始化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];

Nod to CGRect Tricks . 点头CGRect技巧


#5楼

For multiline text the left and the right margin can be set by using NSAttributedString. 对于多行文本,可以使用NSAttributedString设置左边距和右边距。

NSMutableParagraphStyle *style =  [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.alignment = NSTextAlignmentJustified;
style.firstLineHeadIndent = 10.0f;
style.headIndent = 10.0f;
style.tailIndent = -10.0f;   

NSAttributedString *attrText = [[NSAttributedString alloc] initWithString:title attributes:@{ NSParagraphStyleAttributeName : style}];  

UILabel * label = [[UILabel alloc] initWithFrame:someFrame];
label.numberOfLines = 0;
label.attributedText = attrText;

#6楼

For Xamarin users (using Unified API): 对于Xamarin用户(使用Unified API):

class UIMarginLabel : UILabel
{
    public UIMarginLabel()
    {
    }

    public UIMarginLabel( CGRect frame ) : base( frame )
    {
    }

    public UIEdgeInsets Insets { get; set; }

    public override void DrawText( CGRect rect )
    {
        base.DrawText( Insets.InsetRect( rect ) );
    }
}

And for those using the original MonoTouch API: 对于使用原始MonoTouch API的用户:

public class UIMarginLabel : UILabel
{
    public UIEdgeInsets Insets { get; set; }

    public UIMarginLabel() : base()
    {
        Insets = new UIEdgeInsets(0, 0, 0, 0);
    }
    public UIMarginLabel(RectangleF frame) : base(frame)
    {
        Insets = new UIEdgeInsets(0, 0, 0, 0);
    }

    public override void DrawText(RectangleF frame)
    {
        base.DrawText(new RectangleF(
            frame.X + Insets.Left,
            frame.Y + Insets.Top,
            frame.Width - Insets.Left - Insets.Right,
            frame.Height - Insets.Top - Insets.Bottom));
    }
}