NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"smiley_0.png"];
attachment.bounds = CGRectMake(0, 0, 22, 22);
NSMutableAttributedString *attributedString = [[NSAttributedString attributedStringWithAttachment:attachment] mutableCopy];
[aLabel setAttributedText:attributedString];
使用上面的代码,
aLabel
可以正确显示图像(smiley_0.png),现在我想在aLabel
后面附加一个字符串,有什么想法吗?
最佳答案
尝试如下使用NSMutableAttributedString的appendAttributedString到您的attributedString
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"smiley_0.png"];
attachment.bounds = CGRectMake(0, 0, 22, 22);
NSMutableAttributedString *appendedString=[[NSMutableAttributedString alloc]initWithString:@"yourString"];
NSMutableAttributedString *attributedString = [[NSAttributedString attributedStringWithAttachment:attachment] mutableCopy];
[attributedString appendAttributedString:appendedString];
[aLabel setAttributedText:attributedString];
希望它能对您有所帮助。
关于ios - NSMutableAttributedString包含NSMutableAttributedString和NSString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23286629/