2

I'm trying to change the horizontal position of a NSTextAttachment image within a UITextView. I've tried setting

[attributedString setAttributes:@{NSKernAttributeName:@(1.5)} range:NSMakeRange(0, 1)];

This simply makes the attachment disappear while giving me the appropriate kerning space. I've also tried changing the origin x-coordinate in

- (CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex

This has no effect on the horizontal position of the attachment. Please advise.

ethanyu
  • 143
  • 1
  • 6

1 Answers1

0

You can set horizontal position of a NSTextAttachment image by textAttachment.bounds properties.

NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"YourImageURL"] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60];
NSData *urlData=[NSURLConnection sendSynchronousRequest:imageRequest returningResponse:nil error:nil];
UIImage * image = [UIImage imageWithData:urlData];
if (image != nil) {
textAttachment.image = image;
} else {
textAttachment.image =[UIImage imageNamed:@"YourPlaceHolderImageName"];
            }
textAttachment.bounds =  CGRectMake(0,-4, textAttachment.image.size.height, textAttachment.image.size.width);
amitg3
  • 11
  • 4