NSTestAttachment

Posted by zfx5130 on December 25, 2015
NSTextAttachment

NSTextAttachment对象一般与NSAttributedString一起搭配使用,一个NSTextAttachment可以包含NSData, UIImage 或者NSFileWrapper对象,我们通过修改属性来改变数据显示时候的外观。例如。我们需要在文字后面添加图片,这时候,我们就需要用到NSTextAttachment。具体实现如下


  -(void)testNSTextAttachment {
    NSTextAttachment *attchment = [[NSTextAttachment alloc] init];
    attchment.image = [UIImage imageNamed:@"test"];
    //bounds属性能改变图片的位置。
    attchment.bounds = CGRectMake(0, 0, 15, 15);
   
    //这里通过appendAttributedString 添加的先后图字的先后顺序,则是展示的前后顺序。
    NSMutableAttributedString *attachmentString = [[NSMutableAttributedString alloc] initWithAttributedString:[NSMutableAttributedString attributedStringWithAttachment:attchment]];

    [attachmentString appendAttributedString:[[NSAttributedString alloc] initWithString:@"测试测试测试测试测试试"]];
    self.testLabel.attributedText = attachmentString;
}   

Demo

#####