2014-04-25 31 views
1
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包含NSMutableAttributedString和NSString的

+2

你試過追加嗎? – Wain

+0

@多謝了,我試過了,但其他一些問題使它顯示錯誤,現在沒事。 – Mil0R3

回答

3

嘗試使用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]; 

希望它可以幫助你......!

+0

尼斯答案+1。 – Vvk

相關問題