I added NSTextAlignmentRight to the UILabel. I have also given border to UILabel.Now issues is there is no padding in right.
self.lblColumnCaption.textAlignment = NSTextAlignmentRight;
Please suggest how can i add spacing to the right?
I added NSTextAlignmentRight to the UILabel. I have also given border to UILabel.Now issues is there is no padding in right.
self.lblColumnCaption.textAlignment = NSTextAlignmentRight;
Please suggest how can i add spacing to the right?
There are so many solutions are there, but the easiset way is you can put some space after your text. If you don't want that please try below solution,
Simply add the UILabel with no background set to a UIView with the background set. Set the label's width, 10 less than the width of bg view.
UIView* bg = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 70)];
//give border and corner radius for the bg view if you want
UILabel* yourLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, yourWidth-10, yourHeight)];
[bg addSubview:yourLabel];
[self addSubview:bg];
Alternatively, you can solve this by initializing your UILabel
with a custom frame.
CGRect initialFrame = CGRectMake(0, 0, 100, 100);
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0, 0, 0, 10);
CGRect paddedFrame = UIEdgeInsetsInsetRect(initialFrame, contentInsets); self.label =
[[UILabel alloc] initWithFrame:paddedFrame];
As mentioned in other answers the best way is subclassing UILabel and override drawTextInRect method.
If you really don't want to override, you can use an NSAttributedString to add a right padding (tailIndent property) BEFORE setting textAlignment to NSTextAlignmentRight.
CGFloat rightPadding = 5.0f;
NSString *dateString = @"Your Date";
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.tailIndent = -rightPadding;
NSDictionary *attributes = @{NSParagraphStyleAttributeName: style};
label.attributedText = [[NSAttributedString alloc] initWithString:dateString attributes:attributes];
label.textAlignment = NSTextAlignmentRight;
use this simple line of code
txtFullName.layer.sublayerTransform = CATransform3DMakeTranslation(35, 0, 0);//acc to your situation