Skip to content

NSTextAttachment ​

NSTextAttachment represents an attachment in an attributed string: an image shown in the position of one character.

An attachment must become an attributed string before it can be combined with text: use NSAttributedString.attributedStringWithAttachment() to build a one-character attributed string, then splice it into the text.

NSTextAttachment fires no events. Its values are changed only by the app's own code, so there are no change notifications.

Inheritance: EventTarget → NSObject → NSTextAttachment

Constructor ​

NSTextAttachment()

Creates an empty attachment; set image afterward.

Static methods ​

NSTextAttachment.textAttachmentWithImage()

Creates an attachment that shows the given image.

Instance properties ​

Also inherits the properties of NSObject.

PropertyDescription
imageThe image shown
boundsWhere the image is drawn and how large

Instance methods ​

NSTextAttachment has no instance methods of its own.

Examples ​

Inserting a star into text ​

js
const star = UIImage.systemImageNamed('star.fill');
if (star !== null) {
  const attachment = NSTextAttachment.textAttachmentWithImage(star);
  attachment.bounds = new CGRect(0, -3, 18, 18);

  const text = new NSMutableAttributedString('Rating ', { font: UIFont.systemFontOfSize(17) });
  text.appendAttributedString(NSAttributedString.attributedStringWithAttachment(attachment));
  text.appendAttributedString(new NSAttributedString(' 4.8'));

  const label = new UILabel();
  label.attributedText = text;
}

Effect (not verified in the simulator): an 18 × 18 pt star appears between "Rating" and "4.8", 3 pt below the text baseline.

Corresponding iOS API ​

ObjCJS
[NSTextAttachment textAttachmentWithImage:image]NSTextAttachment.textAttachmentWithImage(image)
[NSAttributedString attributedStringWithAttachment:attachment]NSAttributedString.attributedStringWithAttachment(attachment)

See also ​