我正在尝试在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
这对附件的水平位置没有影响。 请指教。
答案 0 :(得分: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);