Last active
January 11, 2017 09:11
-
-
Save phynet/a2329ee1eb6dad235e78e5ea49cba616 to your computer and use it in GitHub Desktop.
Add a image to a UILabel
This file contains 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
NSTextAttachment *attachment = [[NSTextAttachment alloc] init]; | |
attachment.image = [UIImage imageNamed:@"image.png"]; | |
//add image to the right side of the text | |
attachment.bounds = CGRectMake(10, -4, 28,28); | |
//add image to the left side of the text | |
attachment.bounds = CGRectMake(-4, 0, 18,18); | |
NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment]; | |
NSMutableAttributedString *stringWithIcon= [[NSMutableAttributedString alloc] initWithString:@""]; | |
[stringWithIcon appendAttributedString:attachmentString]; | |
NSMutableAttributedString *label = [[NSMutableAttributedString alloc] initWithAttributedString:self.priceLabel.attributedText]; | |
[label appendAttributedString:stringWithIcon]; | |
//this was specific for my case, I needed to add white space to my label in order to show a little margin with constraints | |
NSMutableAttributedString *whiteSpace= [[NSMutableAttributedString alloc] initWithString:@" "]; | |
[label appendAttributedString:whiteSpace]; | |
self.priceLabel.attributedText = label; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment