Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added setDisplaySize:withMaxDisplaySize: to automatically calculate m…

…issing dimensions

Also for #338
  • Loading branch information...
commit 48aad8d4131dd0cd40d39572e653d412f8e0416d 1 parent 99da4df
@Cocoanetics authored
Showing with 49 additions and 22 deletions.
  1. +9 −0 Core/Source/DTTextAttachment.h
  2. +40 −22 Core/Source/DTTextAttachment.m
View
9 Core/Source/DTTextAttachment.h
@@ -102,6 +102,15 @@ typedef enum
@property (nonatomic, assign) CGSize displaySize;
/**
+ Updates the display size optionally passing a maximum size that it should not exceed.
+
+ This method in contrast to using the displaySize property will use the originalSize and max display size to calculate missing dimensions.
+ @param displaySize The new size to display the content with
+ @param maxDisplaySize the maximum size that the content should be scaled to fit
+ */
+- (void)setDisplaySize:(CGSize)displaySize withMaxDisplaySize:(CGSize)maxDisplaySize;
+
+/**
The contents of the receiver
*/
@property (nonatomic, strong) id contents;
View
62 Core/Source/DTTextAttachment.m
@@ -180,38 +180,22 @@ + (DTTextAttachment *)textAttachmentWithElement:(DTHTMLElement *)element options
if ([contentURL isFileURL])
{
DTImage *image = [[DTTextAttachment sharedImageCache] objectForKey:[contentURL path]];
- if (!image) {
+ if (!image)
+ {
image = [[DTImage alloc] initWithContentsOfFile:[contentURL path]];
[[DTTextAttachment sharedImageCache] setObject:image forKey:[contentURL path]];
}
originalSize = image.size;
- // width and/or height missing
- if (displaySize.width==0 && displaySize.height==0)
- {
- displaySize = originalSize;
- }
- else if (!displaySize.width && displaySize.height)
- {
- CGFloat factor = image.size.height / displaySize.height;
- displaySize.width = roundf(image.size.width / factor);
- }
- else if (displaySize.width>0 && displaySize.height==0)
- {
- CGFloat factor = image.size.width / displaySize.width;
- displaySize.height = roundf(image.size.height / factor);
- }
- }
- else
- {
- // remote image, we have to relayout once this size is known
- displaySize = CGSizeMake(1, 1); // one pixel so that loading is triggered
+ // initial display size matches original
+ displaySize = originalSize;
}
}
}
+
// if you have no display size we assume original size
if (CGSizeEqualToSize(displaySize, CGSizeZero))
{
@@ -327,7 +311,41 @@ - (void)setOriginalSize:(CGSize)originalSize
self.displaySize = _originalSize;
}
-/**
+- (void)setDisplaySize:(CGSize)displaySize withMaxDisplaySize:(CGSize)maxDisplaySize
+{
+ if (_originalSize.width && _originalSize.height)
+ {
+ // width and/or height missing
+ if (displaySize.width==0 && displaySize.height==0)
+ {
+ displaySize = _originalSize;
+ }
+ else if (!displaySize.width && displaySize.height)
+ {
+ // width missing, calculate it
+ CGFloat factor = _originalSize.height / displaySize.height;
+ displaySize.width = roundf(_originalSize.width / factor);
+ }
+ else if (displaySize.width>0 && displaySize.height==0)
+ {
+ // height missing, calculate it
+ CGFloat factor = _originalSize.width / displaySize.width;
+ displaySize.height = roundf(_originalSize.height / factor);
+ }
+ }
+
+ if (maxDisplaySize.width>0 && maxDisplaySize.height>0)
+ {
+ if (maxDisplaySize.width < displaySize.width || maxDisplaySize.height < displaySize.height)
+ {
+ displaySize = sizeThatFitsKeepingAspectRatio(displaySize, maxDisplaySize);
+ }
+ }
+
+ _displaySize = displaySize;
+}
+
+/**
Accessor for the contents instance variable. If the content type is DTTextAttachmentTypeImage this returns a DTImage instance of the contents.
@returns Contents. If it is an image, a DTImage instance is returned. Otherwise it is returned as is.
*/
Please sign in to comment.
Something went wrong with that request. Please try again.