Skip to content

Instantly share code, notes, and snippets.

@pilky
Created June 30, 2017 12:57
Show Gist options
  • Save pilky/284f7b18a1c43d05b264a224e825f91e to your computer and use it in GitHub Desktop.
Save pilky/284f7b18a1c43d05b264a224e825f91e to your computer and use it in GitHub Desktop.
Intrinsic size
@property (assign, nonatomic) CGSize preferredMaxSize;
- (CGSize)intrinsicContentSize {
CGSize imageSize = self.image.size;
CGSize maxSize = self.preferredMaxSize;
if (imageSize.height > maxSize.height) {
imageSize.width *= maxSize.height / imageSize.height;
imageSize.height = maxSize.height;
}
if (imageSize.width > maxSize.width) {
imageSize.height *= maxSize.width / imageSize.width;
imageSize.width = maxSize.width;
}
return imageSize;
}
- (void)setImage:(UIImage *)aImage {
[super setImage:aImage];
[self invalidateIntrinsicContentSize];
}
- (void)setPreferredMaxSize:(CGSize)size {
_preferredMaxSize = size;
[self invalidateIntrinsicContentSize];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment