Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add padding-left on a UILabel created programmatically?

Tags:

I know this is a noob question but ...I have these labels on a tableview, but the text is completely squished to the left. I want to add a bit of padding. How do I go about it?

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  {      UIView* customView = [[[UIView alloc] initWithFrame:CGRectMake(10,0,300,60)] autorelease];      UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];      headerLabel.backgroundColor = [UIColor colorWithHexString:[[_months objectAtIndex:section] objectForKey:@"color"]];     headerLabel.font = [UIFont boldSystemFontOfSize:18];     headerLabel.frame = CGRectMake(0,0,400,30);     headerLabel.text =  [[_months objectAtIndex:section] objectForKey:@"name"];      headerLabel.textColor = [UIColor whiteColor];       [customView addSubview:headerLabel];      return customView; } 

any help is much appreciated! Thanks!

like image 764
sixstatesaway Avatar asked Feb 29 '12 15:02

sixstatesaway


People also ask

How do I add padding to UILabel?

If you have created an UILabel programmatically, replace the UILabel class with the PaddingLabel and add the padding: // Init Label let label = PaddingLabel() label. backgroundColor = . black label.

How do I add padding to labels in Xcode?

You should use a container view and place the label inside of that. Then add constraints for the label, which will be equivalent to a padding. Select the label and click the constraints panel at the bottom right in the Storyboard file.

How do I change my UILabel text?

To change the font or the size of a UILabel in a Storyboard or . XIB file, open it in the interface builder. Select the label and then open up the Attribute Inspector (CMD + Option + 5). Select the button on the font box and then you can change your text size or font.

How do you label corner radius in Swift?

masksToBounds = true" is an important code, if you apply corner radius to a label and if it dosen't work than adding this will definitely add the corner radius to a particular label.. So this should be the correct answer..


1 Answers

For a full list of available solutions, see this answer: UILabel text margin


The most flexible approach to add padding to UILabel is to subclass UILabel and add an edgeInsets property. You then set the desired insets and the label will be drawn accordingly.

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)]; }  @end 
like image 127
Brody Robertson Avatar answered Oct 03 '22 00:10

Brody Robertson