ios - NSMutableAttributedString包含NSMutableAttributedString和NSString

标签 ios objective-c nsstring nsattributedstring

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/

相关文章:

ios - Xcode - UTF-8 字符串编码

objective-c - 如何使用 sqlite 在 Xcode 中开始、回滚、提交事务

ios - 如果字符是 4 字节长度的 UTF8,[NSString substringWithRange] 中存在错误?

cocoa - 如何以一定角度绘制 NSString?

ios - 过期的配置文件更新

ios - iOS 中的模型- View - Controller

ios - UserDefaults - 使用 Xcode Simulator 时保存不一致

iphone - 自定义对象的Dealloc

ios - 无法在屏幕上绘制矩形 ios

ios - -[__NSCFString _getValue :forType:]: unrecognized selector sent to instance