Created
January 4, 2017 03:28
-
-
Save yycking/15feec809eeecb1ad0e0ba4bf392dca5 to your computer and use it in GitHub Desktop.
將UILabel中的圖字置中
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
UILabel *label = [[UILabel alloc] initWithFrame:tableView.bounds]; | |
label.numberOfLines = 0; | |
NSMutableAttributedString *attachmentString = [NSMutableAttributedString new]; | |
NSTextAttachment *imageAttachment = [NSTextAttachment new]; | |
imageAttachment.image = [UIImage imageNamed:images[index]]; | |
NSAttributedString *imageString = [NSAttributedString attributedStringWithAttachment:imageAttachment]; | |
[attachmentString appendAttributedString:imageString]; | |
[attachmentString appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]]; | |
NSString *text = NSLocalizedString(tiltes[index], nil); | |
NSAttributedString *textString = [[NSAttributedString alloc] initWithString:text attributes:@{ NSForegroundColorAttributeName : [UIColor lightGrayColor]}]; | |
[attachmentString appendAttributedString:textString]; | |
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new]; | |
paragraphStyle.alignment = NSTextAlignmentCenter; | |
[attachmentString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attachmentString.length)]; | |
label.attributedText = attachmentString; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment