IOS Add Left Indent to UILabel

I need to create a UILabel with a background color, and I would like to add a few remaining additions, but every solution I find seems like an unpleasant hack.

What is the “standard” way to achieve this with iOS 5 ahead?

EDIT

Screenshot illustrating my scenario.

enter image description here

+64
ios uilabel
Oct 17 '13 at 21:24
source share
12 answers

See this answer for a complete list of available solutions: UILabel text fields




Try to subclass UILabel as @Tommy Herbert suggests in response to [this question] [1]. Copied and pasted for your convenience:

I solved this by subclassing UILabel and overriding drawTextInRect: like this:

 - (void)drawTextInRect:(CGRect)rect { UIEdgeInsets insets = {0, 5, 0, 5}; [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)]; } 
+94
Oct 17 '13 at 21:49
source share

add a space character also to the line. that the poor person complements :)

OR

I would go with a custom background, but if you don't want this, space is the only other convenient options I see ...

OR write a custom label. make text through coretext

+24
Oct 17 '13 at 21:43
source share

I know this is old, but for posterity.

The most important part is that you must redefine both intrinsicContentSize () and drawTextInRect () to account for AutoLayout

 var contentInset: UIEdgeInsets = .zero { didSet { setNeedsDisplay() } } override public var intrinsicContentSize: CGSize { let size = super.intrinsicContentSize return CGSize(width: size.width + contentInset.left + contentInset.right, height: size.height + contentInset.top + contentInset.bottom) } override public func drawText(in rect: CGRect) { super.drawText(in: UIEdgeInsetsInsetRect(rect, contentInset)) } 
+24
Apr 20 '16 at 7:21
source share
 #define PADDING 5 @interface MyLabel : UILabel @end @implementation MyLabel - (void)drawTextInRect:(CGRect)rect { return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, UIEdgeInsetsMake(0, PADDING, 0, PADDING))]; } - (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines { return CGRectInset([self.attributedText boundingRectWithSize:CGSizeMake(999, 999) options:NSStringDrawingUsesLineFragmentOrigin context:nil], -PADDING, 0); } @end 
+23
Jan 27 '14 at 9:50
source share
 UIView* bg = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 70)]; bg.backgroundColor = [UIColor blackColor]; UILabel* yourLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, y, yourWidth, yourHeight)]; [bg addSubview:yourLabel]; [self addSubview:bg]; 
+16
Oct 17 '13 at 21:34
source share

It is sometimes convenient to use partial UNICODE spaces to achieve alignment when prototyping. This can be useful in prototyping, proof of concept, or simply to delay the implementation of graphical algorithms.

If you use convenient UNICODE spaces, keep in mind that at least one of the UNICODE spaces has a size based on the font on which it is displayed, in particular the actual space itself (U + 0020, ASCII 32)

If you use the system default font for iOS in UILabel, the characteristics of the system default font may change in the next version of iOS and suddenly introduce an unwanted offset, changing the exact distance of your application. This can and does happen, for example, the San Francisco font replaced the previous iOS system font in the iOS release.

UNICODE is easy to specify in Swift, for example:

 let six_per_em_space = "\u{2006}" 

Alternatively, cut / paste space from an HTML page directly into a UILabel text box in Interface Builder.

Note. The attached image is a screenshot, not HTML, so go to the linked page if you want to cut / paste a space.

UNICODE spaces

+15
Feb 11 '15 at 0:42
source share

I had several problems with the answers here, for example, when you added to the fill, the width of the contents overflowed the box and that I needed a corner in radius. I solved this using the following subclass of UILabel:

 #import "MyLabel.h" #define PADDING 8.0 #define CORNER_RADIUS 4.0 @implementation MyLabel - (void)drawRect:(CGRect)rect { self.layer.masksToBounds = YES; self.layer.cornerRadius = CORNER_RADIUS; UIEdgeInsets insets = {0, PADDING, 0, PADDING}; return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)]; } - (CGSize) intrinsicContentSize { CGSize intrinsicSuperViewContentSize = [super intrinsicContentSize] ; intrinsicSuperViewContentSize.width += PADDING * 2 ; return intrinsicSuperViewContentSize ; } @end 

Hope this helps someone! Please note: if you want to fill in the top and bottom, you will need to change the following lines:

 UIEdgeInsets insets = {0, PADDING, 0, PADDING}; 

For this:

 UIEdgeInsets insets = {PADDING, PADDING, PADDING, PADDING}; 

And add this line under the same for width:

 intrinsicSuperViewContentSize.height += PADDING * 2 ; 
+4
Sep 14 '15 at
source share

One thing I did to overcome this problem was to use UIButton instead of UILabel. Then, in the interface builder attribute inspector, I used Edge for Title as a complement.
If you do not attach the button to the action, it will not be selected when pressed, but it will still show the selection.

You can also do this programmatically with the following code:

 UIButton *mButton = [[UIButton alloc] init]; [mButton setTitleEdgeInsets:UIEdgeInsetsMake(top, left, bottom, right)]; [mButton setTitle:@"Title" forState:UIControlStateNormal]; [self.view addSubView:mButton]; 

This approach gives the same result, but sometimes it didn’t work for some reason that I did not investigate, because, whenever possible, I use Interface Builder.

This is still a workaround, but it works very well if the backlight does not bother you. Hope this is helpful

+2
Jan 02 '15 at 11:11
source share

subclassing UILabel and overriding drawTextInRect: for example:

  - (void)drawTextInRect:(CGRect)rect { UIEdgeInsets insets = {0, 10, 0, 0}; return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)]; } 
+1
Oct. 17 '13 at 9:53 on
source share

If you want to add an add-on to UILabel but don’t want to subclass, you can put your label in a UIView and give paddings with autorun, for example:

Filling with UIView and Autostart

Result:

Result

0
Jul 6 '17 at 10:22
source share

Swift 5

Create a file under the class and label it as a custom class name through the storyboard. It.

 class PaddingLabel: UILabel { override func drawText(in rect: CGRect) { let insets = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)//CGRect.inset(by:) super.drawText(in: rect.inset(by: insets)) } } 
0
May 03 '19 at 9:33
source share

If you need more specific text alignment than adding spaces to the left of the text, you can always add a second blank label, just how much indentation you need.

I have buttons with text aligned to the left with 10px indentation and needing a shortcut below to look in line. He gave the label text and alignment on the left and put it on x = 10, and then made a small second label of the same background color with width = 10 and lined it next to the real label.

Minimal code and looks good. It just makes AutoLayout a little more trouble for everything to work.

-one
Apr 23 '14 at 18:58
source share



All Articles