ios - 更改 NSTextAttachment 图像的大小?

标签 ios objective-c nstextattachment

我正在使用以下代码将 RSS 提要的原始 HTML 格式化为 NSAttributedString . HTML 包含 <img>导致图像附加为 NSTextAttachment 的标签进入NSAttributedString .问题是图像对于放置属性字符串的 UITextView 来说太大了。有没有办法可以从属性字符串中操纵图像的大小?以下是代表图像附件的整个属性字符串的摘录。

{
    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,获取附件、边界并修改它们:

[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;
}];

关于ios - 更改 NSTextAttachment 图像的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31945236/

相关文章:

swift - 将包含图像的 NSTextAttachment 替换为字符串

iOS 在使用 ffmpeg 添加的音频和字幕轨道之间切换

ios - Swift:如何停止暴露委托(delegate)方法

ios - 是否有可能将浮点值存储到 id?

iphone - Objective C 类实例声明错误?

ios - 在 UILabel 中,是否可以强制一条线在某个地方不中断

objective-c - 将带有 NSTextAttachment 的 NSAttributedString 保存到文件中。如何?

IOS 的 C# PushSharp 通知在 IIS 7.5 中不起作用

iOS 触摸事件通知(私有(private) API)

iphone - NSBlockOperation 和启动方法