Skip to content

Instantly share code, notes, and snippets.

@qy1010
Last active June 13, 2019 08:06
Show Gist options
  • Save qy1010/8c1b59db0b6c8fda7b6c806bc9f98d73 to your computer and use it in GitHub Desktop.
Save qy1010/8c1b59db0b6c8fda7b6c806bc9f98d73 to your computer and use it in GitHub Desktop.
有图片文字AttributedString
- (NSAttributedString *)bounusString:(NSString *)bounusText
{
NSMutableAttributedString *msg = [[NSMutableAttributedString alloc] init];
UIFont *textFont = [UIFont fontWithName:@"Roboto-Regular" size:13];
NSTextAttachment *dimondAttachment = [[NSTextAttachment alloc] init];
dimondAttachment.image = [UIImage imageNamed:@"ico_dimond"];
dimondAttachment.bounds = CGRectMake(0, 0, 13, 13);
NSAttributedString *dimondatrri = [NSAttributedString attributedStringWithAttachment:dimondAttachment];
[msg appendAttributedString:dimondatrri];
NSString *bounus = [NSString stringWithFormat:NSLocalizedString(@"xxx", @"xxx"),bounusText];
NSString *bounusString = [NSString stringWithFormat:@" %@ ",bounus];
NSMutableAttributedString *bounes = [[NSMutableAttributedString alloc] initWithString:bounusString];
[bounes addAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#F8E71C"],NSFontAttributeName:textFont} range:NSMakeRange(0, bounes.string.length)];
[msg appendAttributedString:bounes];
NSTextAttachment *yellowAttachment = [[NSTextAttachment alloc] init];
yellowAttachment.image = [[UIImage imageNamed:@"xxx"] rtl_imageFlippedForRightToLeftLayoutDirection];
yellowAttachment.bounds = CGRectMake(0, 0, 10, 10);
NSAttributedString *yellowAtrri = [NSAttributedString attributedStringWithAttachment:yellowAttachment];
[msg appendAttributedString:yellowAtrri];
[msg addAttribute:NSWritingDirectionAttributeName
value:@[ @(NSWritingDirectionEmbedding | isRTLInterface() ? NSWritingDirectionRightToLeft : NSWritingDirectionLeftToRight) ]
range:NSMakeRange(0, msg.length)];
return [msg copy];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment