首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改NSTextAttachment图像的大小?

更改NSTextAttachment图像的大小?
EN

Stack Overflow用户
提问于 2015-08-11 22:56:35
回答 3查看 5.4K关注 0票数 2

我使用以下代码将RSS提要的原始HTML格式化为NSAttributedString。超文本标记语言包含的<img>标记会使图像作为NSTextAttachment附加到NSAttributedString中。问题是图像对于放置属性字符串的UITextView来说太大了。有没有一种方法可以在属性字符串中操作图像的大小?下面是表示图像附件的整个属性字符串的摘录。

代码语言:javascript
复制
{
    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;
}
EN

回答 3

Stack Overflow用户

发布于 2015-11-28 00:10:51

你可以遍历你的NSAttributedString,得到附件,边界和修改它们:

代码语言:javascript
复制
[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;
}];
票数 6
EN

Stack Overflow用户

发布于 2017-06-19 02:13:41

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

代码语言:javascript
复制
        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
EN

Stack Overflow用户

发布于 2016-12-31 15:07:59

这是我的solution.May,可以帮助别人。

代码语言:javascript
复制
[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;
                                                
                     }
                 }
                 
             }]

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31945236

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档