如果您使用
attributeText
属性对textView进行
po
操作,您会发现它有许多类似这样的对象:
NSAttachment = "<NSTextAttachment: 0x6000002a39c0> \"XXXXXXX.jpg\"";
NSColor = "kCGColorSpaceModelRGB 0 0 0 1 ";
NSFont = "<UICTFont: 0x7f8356400f60> font-family: \"Times New Roman\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
NSKern = 0;
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 12, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 15/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (\n), Lists (\n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0";
NSStrokeColor = "kCGColorSpaceModelRGB 0 0 0 1 ";
NSStrokeWidth = 0;
然后你会发现NSTextAttachment
就是Bingo
。所以枚举这些对象,获取NSTextAttachment
并更改其边界:
(我将图像设置为距离屏幕右侧有10像素的边距)
[attributeString enumerateAttribute:NSAttachmentAttributeName inRange:NSMakeRange(0, attributeString.length) options:0 usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
if (value) {
if ([value isKindOfClass:[NSTextAttachment class]]) {
NSTextAttachment *attach = value;
CGFloat scale = SCREEN_WIDTH / attach.bounds.size.width;
CGRect newRect = CGRectMake(attach.bounds.origin.x, attach.bounds.origin.y,SCREEN_WIDTH-10, attach.bounds.size.height*scale);
attach.bounds = newRect;
}
}
}];
NSAttributedString
寻找带属性的NSAttachmentAttributeName
。然后,您可以根据UITextView
的宽度调整图像大小。 - Larme