繁体   English   中英

NSMutableAttributedString包含NSMutableAttributedString和NSString

[英]NSMutableAttributedString contains NSMutableAttributedString and NSString

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用于您的attributionString,如下所示

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];

希望它能帮助你......!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM