简体   繁体   English

更改NSTextAttachment图像的大小?

[英]Change size of NSTextAttachment Image?

I am using the following code to format the raw HTML of an RSS feed into an NSAttributedString . 我使用以下代码将RSS提要的原始HTML格式化为NSAttributedString The HTML contains <img> tags that cause the image to be attached as an NSTextAttachment into the NSAttributedString . HTML包含<img>标记,这些标记使图像作为NSTextAttachment附加到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? 问题是图像对于放置属性字符串的UITextView而言太大了。有没有办法可以在属性字符串中操纵图像的大小? 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;
}

You can iterate over your NSAttributedString, get the attachments, the bounds and modify them: 您可以迭代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;
}];

you can set the bounds of NSTextAttachment, for example(in Swift3): 例如,您可以设置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

Here is my solution.May be will help someone. 这是我的解决方案。可能会帮助别人。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM