本文翻譯自: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));
}
}