更改NSTextAttachment图片的大小? [英] Change size of NSTextAttachment Image?

查看:465
本文介绍了更改NSTextAttachment图片的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码将RSS feed的原始HTML格式设置为NSAttributedString. HTML包含<img>标记,这些标记使图像作为NSTextAttachment附加到NSAttributedString中.问题在于,对于放置属性字符串的UITextView而言,图像太大了.有没有一种方法可以从属性字符串中操纵图像的大小?以下是代表图像附件的整个属性字符串的摘录.

I am using the following code to format the raw HTML of an RSS feed into an NSAttributedString. The HTML contains <img> tags that cause the image to be attached as an NSTextAttachment into the NSAttributedString. The problem is that the image is far too large for the UITextView that the attributed string is placed in. Is there a way I can manipulate the size of the image from within the attributed string? Below is an excerpt from the whole attributed string that represents the image attachment.

{
    NSAttachment = "<NSTextAttachment: 0x16d29450> \"e0e1d483a922419807a2378a7ec031af.jpg\"";
    NSColor = "UIDeviceRGBColorSpace 0 0 0 1";
    NSFont = "<UICTFont: 0x16ef8a40> 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 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (null), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
    NSStrokeColor = "UIDeviceRGBColorSpace 0 0 0 1";
    NSStrokeWidth = 0;
}

推荐答案

您可以遍历NSAttributedString,获取附件,边界并进行修改:

You can iterate over your NSAttributedString, get the attachments, the bounds and modify them:

[myAtrrString enumerateAttribute:NSAttachmentAttributeName inRange:NSMakeRange(0, myAtrrString.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
   if (![value isKindOfClass:[NSTextAttachment class]]) {
      return;
   }
   NSTextAttachment *attachment = (NSTextAttachment*)value;
    CGRect bounds = attachment.bounds;
   // modify bounds
   attachment.bounds = bounds;
}];

这篇关于更改NSTextAttachment图片的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆