Skip to content

Instantly share code, notes, and snippets.

@yycking
Created January 4, 2017 03:28
Show Gist options
  • Save yycking/15feec809eeecb1ad0e0ba4bf392dca5 to your computer and use it in GitHub Desktop.
Save yycking/15feec809eeecb1ad0e0ba4bf392dca5 to your computer and use it in GitHub Desktop.
將UILabel中的圖字置中
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