Created
July 26, 2017 14:48
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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