Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rounded Images #193

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
17 changes: 17 additions & 0 deletions CRToast/CRToastConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,21 @@ extern NSString *const kCRToastImageAlignmentKey;
*/
extern NSString *const kCRToastImageTintKey;

/**
The vertical padding used in this image. If supplied, it will create a padding of the specified amount of px above and below the image.

@note Providing `kCRToastImageContentModeKey` a UIViewContentMode.ScaleToFit is recommended when using this property.
*/
extern NSString *const kCRToastImageVerticalPaddingKey;

/**
The image style to use. If true is supplied, it will make the image render as a circle. Expects a BOOL


@note Providing `kCRToastImageContentModeKey` a UIViewContentMode.ScaleToFit is recommended when using this property.
*/
extern NSString *const kCRToastImageRoundedKey;

/**
BOOL setting whether the CRToast should show a loading indicator in the left image location.
*/
Expand Down Expand Up @@ -438,6 +453,8 @@ extern NSString *const kCRToastCaptureDefaultWindowKey;
@property (nonatomic, readonly) UIViewContentMode imageContentMode;
@property (nonatomic, readonly) CRToastAccessoryViewAlignment imageAlignment;
@property (nonatomic, readonly) UIColor *imageTint;
@property (nonatomic, readonly) CGFloat imageVerticalPadding;
@property (nonatomic, readonly) BOOL imageRounded;
@property (nonatomic, readonly) UIActivityIndicatorViewStyle activityIndicatorViewStyle;
@property (nonatomic, readonly) CRToastAccessoryViewAlignment activityViewAlignment;
@property (nonatomic, readonly) BOOL showActivityIndicator;
Expand Down
20 changes: 20 additions & 0 deletions CRToast/CRToastConfig.m
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ + (instancetype)interactionResponderWithInteractionType:(CRToastInteractionType)
NSString *const kCRToastImageContentModeKey = @"kCRToastImageContentModeKey";
NSString *const kCRToastImageAlignmentKey = @"kCRToastImageAlignmentKey";
NSString *const kCRToastImageTintKey = @"kCRToastImageTintKey";
NSString *const kCRToastImageVerticalPaddingKey = @"kCRToastImageVerticalPadding";
NSString *const kCRToastImageRoundedKey = @"kCRToastImageRounded";
NSString *const kCRToastShowActivityIndicatorKey = @"kCRToastShowActivityIndicatorKey";
NSString *const kCRToastActivityIndicatorViewStyleKey = @"kCRToastActivityIndicatorViewStyleKey";
NSString *const kCRToastActivityIndicatorAlignmentKey = @"kCRToastActivityIndicatorAlignmentKey";
Expand Down Expand Up @@ -277,6 +279,8 @@ + (instancetype)interactionResponderWithInteractionType:(CRToastInteractionType)
static UIViewContentMode kCRImageContentModeDefault = UIViewContentModeCenter;
static CRToastAccessoryViewAlignment kCRImageAlignmentDefault = CRToastAccessoryViewAlignmentLeft;
static UIColor * kCRImageTintDefault = nil;
static CGFloat kCRImageVerticalPaddingDefault = 0;
static BOOL kCRImageRoundedDefault = NO;
static BOOL kCRShowActivityIndicatorDefault = NO;
static UIActivityIndicatorViewStyle kCRActivityIndicatorViewStyleDefault = UIActivityIndicatorViewStyleWhite;
static CRToastAccessoryViewAlignment kCRActivityIndicatorAlignmentDefault = CRToastAccessoryViewAlignmentLeft;
Expand Down Expand Up @@ -351,7 +355,9 @@ + (void)initialize {
kCRToastImageKey : NSStringFromClass([UIImage class]),
kCRToastImageContentModeKey : NSStringFromClass([@(kCRImageContentModeDefault) class]),
kCRToastImageAlignmentKey : NSStringFromClass([@(kCRImageAlignmentDefault) class]),
kCRToastImageRoundedKey : NSStringFromClass([@(kCRImageRoundedDefault) class]),
kCRToastImageTintKey : NSStringFromClass([UIColor class]),
kCRToastImageVerticalPaddingKey : NSStringFromClass([@(kCRImageVerticalPaddingDefault) class]),
kCRToastShowActivityIndicatorKey : NSStringFromClass([@(kCRShowActivityIndicatorDefault) class]),
kCRToastActivityIndicatorViewStyleKey : NSStringFromClass([@(kCRActivityIndicatorViewStyleDefault) class]),
kCRToastActivityIndicatorAlignmentKey : NSStringFromClass([@(kCRActivityIndicatorAlignmentDefault) class]),
Expand Down Expand Up @@ -426,6 +432,8 @@ + (void)setDefaultOptions:(NSDictionary*)defaultOptions {
if (defaultOptions[kCRToastImageContentModeKey]) kCRImageContentModeDefault = [defaultOptions[kCRToastImageContentModeKey] integerValue];
if (defaultOptions[kCRToastImageAlignmentKey]) kCRImageAlignmentDefault = [defaultOptions[kCRToastImageAlignmentKey] integerValue];
if (defaultOptions[kCRToastImageTintKey]) kCRImageTintDefault = defaultOptions[kCRToastImageTintKey];
if (defaultOptions[kCRToastImageRoundedKey]) kCRImageRoundedDefault = [defaultOptions[kCRToastImageRoundedKey] boolValue];
if (defaultOptions[kCRToastImageVerticalPaddingKey]) kCRImageVerticalPaddingDefault = [defaultOptions [kCRToastImageVerticalPaddingKey] integerValue];
if (defaultOptions[kCRToastShowActivityIndicatorKey]) kCRShowActivityIndicatorDefault = [defaultOptions[kCRToastShowActivityIndicatorKey] boolValue];
if (defaultOptions[kCRToastActivityIndicatorViewStyleKey]) kCRActivityIndicatorViewStyleDefault = [defaultOptions[kCRToastActivityIndicatorViewStyleKey] integerValue];
if (defaultOptions[kCRToastActivityIndicatorAlignmentKey]) kCRActivityIndicatorAlignmentDefault = [defaultOptions[kCRToastActivityIndicatorAlignmentKey] integerValue];
Expand Down Expand Up @@ -700,6 +708,18 @@ - (UIColor *)imageTint {
return _options[kCRToastImageTintKey] ?: kCRImageTintDefault;
}

- (CGFloat)imageVerticalPadding {
return _options[kCRToastImageVerticalPaddingKey] ?
[_options[kCRToastImageVerticalPaddingKey] floatValue] :
kCRImageVerticalPaddingDefault;
}

- (BOOL)imageRounded {
return _options[kCRToastImageRoundedKey] ?
[_options[kCRToastImageRoundedKey] boolValue] :
kCRImageRoundedDefault;
}

- (BOOL)showActivityIndicator {
return _options[kCRToastShowActivityIndicatorKey] ? [_options[kCRToastShowActivityIndicatorKey] boolValue] : kCRShowActivityIndicatorDefault;
}
Expand Down
40 changes: 31 additions & 9 deletions CRToast/CRToastView.m
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,44 @@ - (void)layoutSubviews {
CGRect contentFrame = self.bounds;
CGSize imageSize = self.imageView.image.size;
CGFloat preferredPadding = self.toast.preferredPadding;

CGFloat imageVerticalPadding = self.toast.imageVerticalPadding;
NSLog(@"vertical padding: %f", imageVerticalPadding); // TODO: Remove
BOOL imageRounded = self.toast.imageRounded;

CGFloat statusBarYOffset = self.toast.displayUnderStatusBar ? (CRGetStatusBarHeight()+CRStatusBarViewUnderStatusBarYOffsetAdjustment) : 0;
contentFrame.size.height = CGRectGetHeight(contentFrame) - statusBarYOffset;

self.backgroundView.frame = self.bounds;

CGFloat imageXOffset = CRImageViewFrameXOffsetForAlignment(self.toast.imageAlignment, preferredPadding, contentFrame.size);
self.imageView.frame = CGRectMake(imageXOffset,
statusBarYOffset,
imageSize.width == 0 ?
0 :
CGRectGetHeight(contentFrame),
imageSize.height == 0 ?
0 :
CGRectGetHeight(contentFrame));



if (imageRounded == YES) {
// Force the image to be a square
CGFloat dimension = self.imageView.frame.size.height > 0 ? self.imageView.frame.size.height : CGRectGetHeight(contentFrame) - imageVerticalPadding*2;
self.imageView.frame = CGRectMake(imageXOffset,
statusBarYOffset+imageVerticalPadding,
dimension,
dimension);

// Set it as rounded
self.imageView.layer.cornerRadius = dimension/2;
self.imageView.clipsToBounds = YES;
} else {
self.imageView.frame = CGRectMake(imageXOffset,
statusBarYOffset+imageVerticalPadding,
imageSize.width == 0 ?
0 :
CGRectGetHeight(contentFrame),
imageSize.height == 0 ?
0 :
CGRectGetHeight(contentFrame) - (imageVerticalPadding*2));

// Only clip to bounds when the image has padding
self.imageView.clipsToBounds = imageVerticalPadding > 0;
}

CGFloat imageWidth = imageSize.width == 0 ? 0 : CGRectGetMaxX(_imageView.frame);
CGFloat x = CRContentXOffsetForViewAlignmentAndWidth(self.toast.imageAlignment, imageXOffset, imageWidth, preferredPadding);

Expand Down
9 changes: 2 additions & 7 deletions Example/CRToastDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,6 @@
attributes = {
LastUpgradeCheck = 0700;
ORGANIZATIONNAME = "Collin Ruffenach";
TargetAttributes = {
5E2FFB5218367C01003333F8 = {
DevelopmentTeam = 7AM27DZVQU;
};
};
};
buildConfigurationList = 5E2FFB4E18367C01003333F8 /* Build configuration list for PBXProject "CRToastDemo" */;
compatibilityVersion = "Xcode 3.2";
Expand Down Expand Up @@ -382,7 +377,7 @@
INFOPLIST_FILE = "CRToastDemo/CRToastDemo-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_BUNDLE_IDENTIFIER = CRToastDemo.self;
PRODUCT_NAME = CRToastDemo;
PROVISIONING_PROFILE = "";
RUN_CLANG_STATIC_ANALYZER = YES;
Expand All @@ -407,7 +402,7 @@
INFOPLIST_FILE = "CRToastDemo/CRToastDemo-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_BUNDLE_IDENTIFIER = CRToastDemo.self;
PRODUCT_NAME = CRToastDemo;
PROVISIONING_PROFILE = "";
RUN_CLANG_STATIC_ANALYZER = YES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@
"size" : "29x29",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "60x60",
Expand Down Expand Up @@ -49,6 +59,11 @@
"idiom" : "ipad",
"size" : "76x76",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "83.5x83.5",
"scale" : "2x"
}
],
"info" : {
Expand Down
6 changes: 6 additions & 0 deletions Example/CRToastDemo/Images.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "Unknown.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 15 additions & 2 deletions Example/CRToastDemo/MainViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ @interface MainViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UISwitch *imageTintEnabledSwitch;
@property (weak, nonatomic) IBOutlet UISlider *imageTintSlider;
@property (weak, nonatomic) IBOutlet UISlider *imageVerticalPaddingSlider;
@property (weak, nonatomic) IBOutlet UILabel *lblImageVerticalPadding;

@property (weak, nonatomic) IBOutlet UISwitch *showImageSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *roundedImageSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *showActivityIndicatorSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *coverNavBarSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *slideOverSwitch;
Expand Down Expand Up @@ -132,10 +135,18 @@ - (IBAction)sliderImageTintChanged:(UISlider *)sender {
[self updateImageTintSwitch];
}

- (IBAction)sliderImageVerticalPaddingChanged:(UISlider *)sender {
[self updateVerticalPaddingLabel];
}

- (void)updateImageTintSwitch {
self.imageTintEnabledSwitch.onTintColor = [UIColor colorWithHue:self.imageTintSlider.value saturation:1.0 brightness:1.0 alpha:1.0];
}

- (void)updateVerticalPaddingLabel {
self.lblImageVerticalPadding.text = [NSString stringWithFormat:@"%d", (int)roundf(self.imageVerticalPaddingSlider.value)];
}

- (IBAction)statusBarChanged:(UISwitch *)sender {
[self setNeedsStatusBarAppearanceUpdate];
}
Expand Down Expand Up @@ -217,9 +228,11 @@ - (NSDictionary*)options {
kCRToastAnimationOutTypeKey : @(CRToastAnimationTypeFromSegmentedControl(_outAnimationTypeSegmentedControl)),
kCRToastAnimationInDirectionKey : @(self.segFromDirection.selectedSegmentIndex),
kCRToastAnimationOutDirectionKey : @(self.segToDirection.selectedSegmentIndex),
kCRToastNotificationPreferredPaddingKey : @(self.sliderPadding.value)} mutableCopy];
kCRToastNotificationPreferredPaddingKey : @(self.sliderPadding.value),
kCRToastImageRoundedKey : @(self.roundedImageSwitch.on),
kCRToastImageVerticalPaddingKey : @(self.imageVerticalPaddingSlider.value)} mutableCopy];
if (self.showImageSwitch.on) {
options[kCRToastImageKey] = [UIImage imageNamed:@"alert_icon.png"];
options[kCRToastImageKey] = [UIImage imageNamed:@"test_picture"];
options[kCRToastImageAlignmentKey] = @(CRToastViewAlignmentForSegmentedControl(self.imageAlignmentSegmentedControl));
}

Expand Down
Loading