Skip to content

Commit

Permalink
Merge pull request cruffenach#166 from jadar/image-alignment-fix
Browse files Browse the repository at this point in the history
User specified padding
  • Loading branch information
Ashton-W committed Jul 19, 2015
2 parents 9098060 + 83296eb commit e01c7a6
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 51 deletions.
6 changes: 6 additions & 0 deletions CRToast/CRToast.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ extern NSString *const kCRToastNotificationTypeKey;
*/
extern NSString *const kCRToastNotificationPreferredHeightKey;

/**
The general preferred padding for the notification.
*/
extern NSString *const kCRToastNotificationPreferredPaddingKey;

/**
The presentation type for the notification. Expects type `CRToastPresentationType`.
*/
Expand Down Expand Up @@ -389,6 +394,7 @@ extern NSString *const kCRToastCaptureDefaultWindowKey;

@property (nonatomic, readonly) CRToastType notificationType;
@property (nonatomic, assign) CGFloat preferredHeight;
@property (nonatomic, assign) CGFloat preferredPadding;
@property (nonatomic, readonly) CRToastPresentationType presentationType;
@property (nonatomic, readonly) BOOL displayUnderStatusBar;
@property (nonatomic, readonly) BOOL shouldKeepNavigationBarBorder;
Expand Down
10 changes: 10 additions & 0 deletions CRToast/CRToast.m
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ + (instancetype)interactionResponderWithInteractionType:(CRToastInteractionType)

NSString *const kCRToastNotificationTypeKey = @"kCRToastNotificationTypeKey";
NSString *const kCRToastNotificationPreferredHeightKey = @"kCRToastNotificationPreferredHeightKey";
NSString *const kCRToastNotificationPreferredPaddingKey = @"kCRToastNotificationPreferredPaddingKey";
NSString *const kCRToastNotificationPresentationTypeKey = @"kCRToastNotificationPresentationTypeKey";

NSString *const kCRToastUnderStatusBarKey = @"kCRToastUnderStatusBarKey";
Expand Down Expand Up @@ -234,6 +235,7 @@ + (instancetype)interactionResponderWithInteractionType:(CRToastInteractionType)

static CRToastType kCRNotificationTypeDefault = CRToastTypeStatusBar;
static CGFloat kCRNotificationPreferredHeightDefault = 0;
static CGFloat kCRNotificationPreferredPaddingDefault = 0;
static CRToastPresentationType kCRNotificationPresentationTypeDefault = CRToastPresentationTypePush;
static BOOL kCRDisplayUnderStatusBarDefault = NO;
static BOOL kCRToastKeepNavigationBarBorderDefault = YES;
Expand Down Expand Up @@ -307,6 +309,7 @@ + (void)initialize {

kCRToastKeyClassMap = @{kCRToastNotificationTypeKey : NSStringFromClass([@(kCRNotificationTypeDefault) class]),
kCRToastNotificationPreferredHeightKey : NSStringFromClass([@(kCRNotificationPreferredHeightDefault) class]),
kCRToastNotificationPreferredPaddingKey : NSStringFromClass([@(kCRNotificationPreferredPaddingDefault) class]),
kCRToastNotificationPresentationTypeKey : NSStringFromClass([@(kCRNotificationPresentationTypeDefault) class]),
kCRToastUnderStatusBarKey : NSStringFromClass([@(kCRDisplayUnderStatusBarDefault) class]),
kCRToastKeepNavigationBarBorderKey : NSStringFromClass([@(kCRToastKeepNavigationBarBorderDefault) class]),
Expand Down Expand Up @@ -377,6 +380,7 @@ + (void)setDefaultOptions:(NSDictionary*)defaultOptions {
//TODO Validate Types of Default Options
if (defaultOptions[kCRToastNotificationTypeKey]) kCRNotificationTypeDefault = [defaultOptions[kCRToastNotificationTypeKey] integerValue];
if (defaultOptions[kCRToastNotificationPreferredHeightKey]) kCRNotificationPreferredHeightDefault = [defaultOptions[kCRToastNotificationPreferredHeightKey] floatValue];
if (defaultOptions[kCRToastNotificationPreferredPaddingKey]) kCRNotificationPreferredPaddingDefault = [defaultOptions[kCRToastNotificationPreferredPaddingKey] floatValue];
if (defaultOptions[kCRToastNotificationPresentationTypeKey]) kCRNotificationPresentationTypeDefault = [defaultOptions[kCRToastNotificationPresentationTypeKey] integerValue];
if (defaultOptions[kCRToastIdentifierKey]) kCRToastIdentifer = defaultOptions[kCRToastIdentifierKey];

Expand Down Expand Up @@ -532,6 +536,12 @@ - (CGFloat)preferredHeight {
kCRNotificationPreferredHeightDefault;
}

- (CGFloat)preferredPadding {
return _options[kCRToastNotificationPreferredPaddingKey] ?
[_options[kCRToastNotificationPreferredPaddingKey] floatValue] :
kCRNotificationPreferredPaddingDefault;
}

- (CRToastPresentationType)presentationType {
return _options[kCRToastNotificationPresentationTypeKey] ?
[self.options[kCRToastNotificationPresentationTypeKey] integerValue] :
Expand Down
3 changes: 2 additions & 1 deletion CRToast/CRToastView.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
@param fullContentWidth full width of contentView to fill
@param fullContentHeight full height of the content view. It is assumed the image & activity indicators frame is a square with sides the length of the height of the contentView.
@param preferredPadding preferred padding to use to lay out the view.
@param showingImage @c YES if an image is being shown and should be accounted for. @c NO otherwise.
@param imageAlignment alignment of image. Only used if @c showingImage is set to @c YES
@param showingActivityIndicator @c YES if an activity indicator is being shown and should be accounted for. @c NO otherwise.
@param activityIndicatorAlignment alignment of activity indicator. Only used if @c showingActivityIndicator is set to @c YES
*/
CGFloat CRContentWidthForAccessoryViewsWithAlignments(CGFloat fullContentWidth, CGFloat fullContentHeight, BOOL showingImage, CRToastAccessoryViewAlignment imageAlignment, BOOL showingActivityIndicator, CRToastAccessoryViewAlignment activityIndicatorAlignment);
CGFloat CRContentWidthForAccessoryViewsWithAlignments(CGFloat fullContentWidth, CGFloat fullContentHeight, CGFloat preferredPadding, BOOL showingImage, CRToastAccessoryViewAlignment imageAlignment, BOOL showingActivityIndicator, CRToastAccessoryViewAlignment activityIndicatorAlignment);

@interface CRToastView : UIView
@property (nonatomic, strong) CRToast *toast;
Expand Down
50 changes: 30 additions & 20 deletions CRToast/CRToastView.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,55 +20,63 @@ @interface CRToastView ()

static CGFloat const CRStatusBarViewUnderStatusBarYOffsetAdjustment = -5;

static CGFloat CRImageViewFrameXOffsetForAlignment(CRToastAccessoryViewAlignment alignment, CGSize contentSize) {
static CGFloat CRImageViewFrameXOffsetForAlignment(CRToastAccessoryViewAlignment alignment, CGFloat preferredPadding, CGSize contentSize) {
CGFloat imageSize = contentSize.height;
CGFloat xOffset = 0;

if (alignment == CRToastAccessoryViewAlignmentLeft) {
xOffset = 0;
xOffset = preferredPadding;
} else if (alignment == CRToastAccessoryViewAlignmentCenter) {
// Calculate mid point of contentSize, then offset for x for full image width
// that way center of image will be center of content view
xOffset = (contentSize.width / 2) - (imageSize / 2);
} else if (alignment == CRToastAccessoryViewAlignmentRight) {
xOffset = contentSize.width - imageSize;
xOffset = contentSize.width - preferredPadding - imageSize;
}

return xOffset;
}

static CGFloat CRContentXOffsetForViewAlignmentAndWidth(CRToastAccessoryViewAlignment alignment, CGFloat width) {
return (width == 0 || alignment != CRToastAccessoryViewAlignmentLeft) ?
kCRStatusBarViewNoImageLeftContentInset :
width;
static CGFloat CRContentXOffsetForViewAlignmentAndWidth(CRToastAccessoryViewAlignment imageAlignment, CGFloat imageXOffset, CGFloat imageWidth, CGFloat preferredPadding) {
return ((imageWidth == 0 || imageAlignment != CRToastAccessoryViewAlignmentLeft) ?
kCRStatusBarViewNoImageLeftContentInset + preferredPadding :
imageXOffset + imageWidth);
}

static CGFloat CRToastWidthOfViewWithAlignment(CGFloat height, BOOL showing, CRToastAccessoryViewAlignment alignment) {
static CGFloat CRToastWidthOfViewWithAlignment(CGFloat height, BOOL showing, CRToastAccessoryViewAlignment alignment, CGFloat preferredPadding) {
return (!showing || alignment == CRToastAccessoryViewAlignmentCenter) ?
0 :
height;
preferredPadding + height + preferredPadding;
}

CGFloat CRContentWidthForAccessoryViewsWithAlignments(CGFloat fullContentWidth, CGFloat fullContentHeight, BOOL showingImage, CRToastAccessoryViewAlignment imageAlignment, BOOL showingActivityIndicator, CRToastAccessoryViewAlignment activityIndicatorAlignment) {
CGFloat CRContentWidthForAccessoryViewsWithAlignments(CGFloat fullContentWidth,
CGFloat fullContentHeight,
CGFloat preferredPadding,
BOOL showingImage,
CRToastAccessoryViewAlignment imageAlignment,
BOOL showingActivityIndicator,
CRToastAccessoryViewAlignment activityIndicatorAlignment)
{
CGFloat width = fullContentWidth;

width -= CRToastWidthOfViewWithAlignment(fullContentHeight, showingImage, imageAlignment);
width -= CRToastWidthOfViewWithAlignment(fullContentHeight, showingActivityIndicator, activityIndicatorAlignment);

if (imageAlignment == activityIndicatorAlignment && showingActivityIndicator && showingImage) {
width += fullContentWidth;
return fullContentWidth;
}

width -= CRToastWidthOfViewWithAlignment(fullContentHeight, showingImage, imageAlignment, preferredPadding);
width -= CRToastWidthOfViewWithAlignment(fullContentHeight, showingActivityIndicator, activityIndicatorAlignment, preferredPadding);

if (!showingImage && !showingActivityIndicator) {
width -= (kCRStatusBarViewNoImageLeftContentInset + kCRStatusBarViewNoImageRightContentInset);
width -= (preferredPadding + preferredPadding);
}

return width;
}

static CGFloat CRCenterXForActivityIndicatorWithAlignment(CRToastAccessoryViewAlignment alignment, CGFloat viewWidth, CGFloat contentWidth) {
static CGFloat CRCenterXForActivityIndicatorWithAlignment(CRToastAccessoryViewAlignment alignment, CGFloat viewWidth, CGFloat contentWidth, CGFloat preferredPadding) {
CGFloat center = 0;
CGFloat offset = viewWidth / 2;
CGFloat offset = viewWidth / 2 + preferredPadding;

switch (alignment) {
case CRToastAccessoryViewAlignmentLeft:
Expand Down Expand Up @@ -121,13 +129,14 @@ - (void)layoutSubviews {
[super layoutSubviews];
CGRect contentFrame = self.bounds;
CGSize imageSize = self.imageView.image.size;
CGFloat preferredPadding = self.toast.preferredPadding;

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, contentFrame.size);
CGFloat imageXOffset = CRImageViewFrameXOffsetForAlignment(self.toast.imageAlignment, preferredPadding, contentFrame.size);
self.imageView.frame = CGRectMake(imageXOffset,
statusBarYOffset,
imageSize.width == 0 ?
Expand All @@ -138,15 +147,15 @@ - (void)layoutSubviews {
CGRectGetHeight(contentFrame));

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

if (self.toast.showActivityIndicator) {
CGFloat centerX = CRCenterXForActivityIndicatorWithAlignment(self.toast.activityViewAlignment, CGRectGetHeight(contentFrame), CGRectGetWidth(contentFrame));
CGFloat centerX = CRCenterXForActivityIndicatorWithAlignment(self.toast.activityViewAlignment, CGRectGetHeight(contentFrame), CGRectGetWidth(contentFrame), preferredPadding);
self.activityIndicator.center = CGPointMake(centerX,
CGRectGetMidY(contentFrame) + statusBarYOffset);

[self.activityIndicator startAnimating];
x = MAX(CRContentXOffsetForViewAlignmentAndWidth(self.toast.activityViewAlignment, CGRectGetHeight(contentFrame)), x);
x = MAX(CRContentXOffsetForViewAlignmentAndWidth(self.toast.activityViewAlignment, imageXOffset, CGRectGetHeight(contentFrame), preferredPadding), x);

[self bringSubviewToFront:self.activityIndicator];
}
Expand All @@ -155,6 +164,7 @@ - (void)layoutSubviews {

CGFloat width = CRContentWidthForAccessoryViewsWithAlignments(CGRectGetWidth(contentFrame),
CGRectGetHeight(contentFrame),
preferredPadding,
showingImage,
self.toast.imageAlignment,
self.toast.showActivityIndicator,
Expand Down
13 changes: 12 additions & 1 deletion Example/CRToastDemo/MainViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ @interface MainViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UISegmentedControl *activityIndicatorAlignmentSegementControl;

@property (weak, nonatomic) IBOutlet UISlider *sliderDuration;
@property (weak, nonatomic) IBOutlet UISlider *sliderPadding;
@property (weak, nonatomic) IBOutlet UILabel *lblDuration;
@property (weak, nonatomic) IBOutlet UILabel *lblPadding;


@property (weak, nonatomic) IBOutlet UISwitch *showImageSwitch;
Expand Down Expand Up @@ -110,10 +112,18 @@ - (void)updateDurationLabel {
self.lblDuration.text = [NSString stringWithFormat:@"%.1f seconds", self.sliderDuration.value];
}

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

- (IBAction)sliderDurationChanged:(UISlider *)sender {
[self updateDurationLabel];
}

- (IBAction)sliderPaddingChanged:(UISlider *)sender {
[self updatePaddingLabel];
}

- (IBAction)statusBarChanged:(UISwitch *)sender {
[self setNeedsStatusBarAppearanceUpdate];
}
Expand Down Expand Up @@ -194,7 +204,8 @@ - (NSDictionary*)options {
kCRToastAnimationInTypeKey : @(CRToastAnimationTypeFromSegmentedControl(_inAnimationTypeSegmentedControl)),
kCRToastAnimationOutTypeKey : @(CRToastAnimationTypeFromSegmentedControl(_outAnimationTypeSegmentedControl)),
kCRToastAnimationInDirectionKey : @(self.segFromDirection.selectedSegmentIndex),
kCRToastAnimationOutDirectionKey : @(self.segToDirection.selectedSegmentIndex)} mutableCopy];
kCRToastAnimationOutDirectionKey : @(self.segToDirection.selectedSegmentIndex),
kCRToastNotificationPreferredPaddingKey : @(self.sliderPadding.value)} mutableCopy];
if (self.showImageSwitch.on) {
options[kCRToastImageKey] = [UIImage imageNamed:@"alert_icon.png"];
options[kCRToastImageAlignmentKey] = @(CRToastViewAlignmentForSegmentedControl(self.imageAlignmentSegmentedControl));
Expand Down
Loading

0 comments on commit e01c7a6

Please sign in to comment.