I want to align the NSTextAttachment to the center of the text, so I set NSBaselineOffsetAttributeName
I finally solve it by adding another attribute NSParagraphStyleAttributeName
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.minimumLineHeight = lineheight; //line height equals to image height
[attributes setValue:paragraphStyle forKey:NSParagraphStyleAttributeName];
don't change the image's offset, just set the text's offset.