Skip to content

Instantly share code, notes, and snippets.

@e23z
Created July 26, 2017 14:48
Show Gist options
  • Save e23z/74202b2944d6143a31874f65c78d817b to your computer and use it in GitHub Desktop.
Save e23z/74202b2944d6143a31874f65c78d817b to your computer and use it in GitHub Desktop.
[UILabel Text Padding] How to apply padding to a UILabel view. #ios #objectivec #ui #swift
// Option 1
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {0, 5, 0, 5};
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
// Option 2
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;
override func drawText(in rect: CGRect) {
let insets = UIEdgeInsets.init(top: 0, left: 5, bottom: 0, right: 5)
super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment