【问题标题】:Change size of NSTextAttachment Image?更改 NSTextAttachment 图像的大小?
【发布时间】:2015-08-11 14:56:35
【问题描述】:

我正在使用以下代码将 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;
}

【问题讨论】:

    标签: ios objective-c nstextattachment


    【解决方案1】:

    您可以遍历您的 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;
    }];
    

    【讨论】:

      【解决方案2】:

      你可以设置 NSTextAttachment 的边界,例如(在 Swift3 中):

              let attachment = NSTextAttachment()
              attachment.image = UIImage(named: "info.png")
              attachment.bounds = CGRect(x: 5, y: -2, width: 15, height: 15)
              let attachmentString = NSAttributedString(attachment: attachment)
      
      
              let attributedString = NSMutableAttributedString(string: "My String")
              attributedString.append(attachmentString)
              labelText.attributedText = attributedString
      

      【讨论】:

        【解决方案3】:

        这是我的解决方案。可能会对某人有所帮助。

        [yourSting enumerateAttribute:NSAttachmentAttributeName
                                    inRange:NSMakeRange(0, [yourSting length])
                                    options:0
                                 usingBlock:^(id value, NSRange range, BOOL *stop)
                     {
                         
                         if ([value isKindOfClass:[NSTextAttachment class]])
                         {
                             NSTextAttachment *attachment = (NSTextAttachment *)value;
                             UIImage *image = nil;
                             if ([attachment image])
                                 image = [attachment image];
                             else
                                 image = [attachment imageForBounds:[attachment bounds]
                                                      textContainer:nil
                                                     characterIndex:range.location];
                             
                             CGSize size = image.size;
                             if(size.width > kSCREEN_WIDTH - 10){
        
                                 // calculate proportional height to width
                                 float ratio = image.size.width /( kSCREEN_WIDTH -10);
                                 float height = image.size.height / ratio;
                                
                                 size = CGSizeMake(kSCREEN_WIDTH - 10, height);
                                 
                                 attachment.bounds = CGRectMake(0, 0, size.width, size.height);
                                 attachment.image = image;
                                                        
                             }
                         }
                         
                     }]

        【讨论】:

        • 这对我来说很完美。在 NSTextView (macOS) 中显示我的属性字符串和使用 [string drawInRect:...] 打印时。
        猜你喜欢
        • 2017-12-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-31
        • 2021-12-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多