Skip to content

Instantly share code, notes, and snippets.

@MaximAlien
Last active May 24, 2019 15:08
Show Gist options
  • Save MaximAlien/e16ce04d66b91c8d08cc to your computer and use it in GitHub Desktop.
Save MaximAlien/e16ce04d66b91c8d08cc to your computer and use it in GitHub Desktop.
[iOS] Attributed string for UIButton
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"test1"];
[attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, [attributeString length])];
[attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0f] range:NSMakeRange(0, [attributeString length])];
NSMutableAttributedString *attributeNewString = [[NSMutableAttributedString alloc] initWithString:@"test2"];
[attributeString appendAttributedString:attributeNewString];
self.testButton.titleLabel.attributedText = attributeString;
// Image attachment
NSTextAttachment *imageAttachment = [[NSTextAttachment alloc] init];
imageAttachment.bounds = CGRectMake(0, 0, 40, 40);
imageAttachment.image = [UIImage imageNamed:@"partly_cloudy"];
NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:imageAttachment];
[attributeString appendAttributedString:attachmentString];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment