2

次のコードを使用して、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;
}
4

3 に答える 3

6

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;
}];
于 2015-11-27T16:10:51.483 に答える
1

これが私の解決策です。誰かを助けるかもしれません。

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

于 2016-12-31T07:07:59.470 に答える