2

我正在尝试更改 UITextView 中 NSTextAttachment 图像的水平位置。我试过设置

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

这只会使附件消失,同时给我适当的字距调整空间。我也试过改变原点x坐标

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

这对附件的水平位置没有影响。请指教。

4

1 回答 1

0

您可以通过 textAttachment.bounds 属性设置 NSTextAttachment 图像的水平位置。

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);
于 2016-06-30T11:11:26.990 回答