Created
June 30, 2017 12:57
-
-
Save pilky/284f7b18a1c43d05b264a224e825f91e to your computer and use it in GitHub Desktop.
Intrinsic size
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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