From 9e3c0915f08bdb1644b06e4a59bda5a3a9caa212 Mon Sep 17 00:00:00 2001 From: Jared Allen Date: Tue, 15 Jan 2013 10:25:18 -0600 Subject: [PATCH 1/6] added private method to generate array of UILabels Signed-off-by: Jared Allen --- PopoverView/PopoverView.m | 88 +++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 46 deletions(-) diff --git a/PopoverView/PopoverView.m b/PopoverView/PopoverView.m index bf7fe88..515e660 100644 --- a/PopoverView/PopoverView.m +++ b/PopoverView/PopoverView.m @@ -10,6 +10,17 @@ #import "PopoverView_Configuration.h" #import +#pragma mark - Private Interface + +@interface PopoverView() + +- (NSMutableArray *)_makeTempViewsWithStrings:(NSArray *)stringArray andImages:(NSArray *)imageArray; + +// Resturns a mutable array for UILabels generated from a NSArray of NSStrings +- (NSMutableArray *)_makeLabelsWithStrings:(NSArray *)stringArray; + +@end + #pragma mark - Implementation @implementation PopoverView @@ -351,52 +362,12 @@ - (void)showAtPoint:(CGPoint)point inView:(UIView *)view withTitle:(NSString *)t - (void)showAtPoint:(CGPoint)point inView:(UIView *)view withStringArray:(NSArray *)stringArray { - NSMutableArray *labelArray = [[NSMutableArray alloc] initWithCapacity:stringArray.count]; - - UIFont *font = kTextFont; - - for (NSString *string in stringArray) { - CGSize textSize = [string sizeWithFont:font]; - UIButton *textButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, textSize.width, textSize.height)]; - textButton.backgroundColor = [UIColor clearColor]; - textButton.titleLabel.font = font; - textButton.titleLabel.textAlignment = kTextAlignment; - textButton.titleLabel.textColor = kTextColor; - [textButton setTitle:string forState:UIControlStateNormal]; - textButton.layer.cornerRadius = 4.f; - [textButton setTitleColor:kTextColor forState:UIControlStateNormal]; - [textButton setTitleColor:kTextHighlightColor forState:UIControlStateHighlighted]; - [textButton addTarget:self action:@selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside]; - - [labelArray addObject:[textButton autorelease]]; - } - - [self showAtPoint:point inView:view withViewArray:[labelArray autorelease]]; + [self showAtPoint:point inView:view withViewArray:[self _makeLabelsWithStrings:stringArray]]; } - (void)showAtPoint:(CGPoint)point inView:(UIView *)view withTitle:(NSString *)title withStringArray:(NSArray *)stringArray - { - NSMutableArray *labelArray = [[NSMutableArray alloc] initWithCapacity:stringArray.count]; - - UIFont *font = kTextFont; - - for (NSString *string in stringArray) { - CGSize textSize = [string sizeWithFont:font]; - UIButton *textButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, textSize.width, textSize.height)]; - textButton.backgroundColor = [UIColor clearColor]; - textButton.titleLabel.font = font; - textButton.titleLabel.textAlignment = kTextAlignment; - textButton.titleLabel.textColor = kTextColor; - [textButton setTitle:string forState:UIControlStateNormal]; - textButton.layer.cornerRadius = 4.f; - [textButton setTitleColor:kTextColor forState:UIControlStateNormal]; - [textButton setTitleColor:kTextHighlightColor forState:UIControlStateHighlighted]; - [textButton addTarget:self action:@selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside]; - - [labelArray addObject:[textButton autorelease]]; - } - - [self showAtPoint:point inView:view withTitle:title withViewArray:[labelArray autorelease]]; +{ + [self showAtPoint:point inView:view withTitle:title withViewArray:[self _makeLabelsWithStrings:stringArray]]; } - (void)showAtPoint:(CGPoint)point inView:(UIView *)view withStringArray:(NSArray *)stringArray withImageArray:(NSArray *)imageArray @@ -405,7 +376,7 @@ - (void)showAtPoint:(CGPoint)point inView:(UIView *)view withStringArray:(NSArra //We create an array of subviews that contains the strings and images centered above a label. NSAssert((stringArray.count == imageArray.count), @"stringArray.count should equal imageArray.count"); - NSMutableArray* tempViewArray = [self makeTempViewsWithStrings:stringArray andImages:imageArray]; + NSMutableArray* tempViewArray = [self _makeTempViewsWithStrings:stringArray andImages:imageArray]; [self showAtPoint:point inView:view withViewArray:[tempViewArray autorelease]]; } @@ -413,12 +384,12 @@ - (void)showAtPoint:(CGPoint)point inView:(UIView *)view withStringArray:(NSArra - (void)showAtPoint:(CGPoint)point inView:(UIView *)view withTitle:(NSString *)title withStringArray:(NSArray *)stringArray withImageArray:(NSArray *)imageArray { NSAssert((stringArray.count == imageArray.count), @"stringArray.count should equal imageArray.count"); - NSMutableArray* tempViewArray = [self makeTempViewsWithStrings:stringArray andImages:imageArray]; + NSMutableArray* tempViewArray = [self _makeTempViewsWithStrings:stringArray andImages:imageArray]; [self showAtPoint:point inView:view withTitle:title withViewArray:[tempViewArray autorelease]]; } -- (NSMutableArray*) makeTempViewsWithStrings:(NSArray *)stringArray andImages:(NSArray *)imageArray +- (NSMutableArray *)_makeTempViewsWithStrings:(NSArray *)stringArray andImages:(NSArray *)imageArray { NSMutableArray *tempViewArray = [[NSMutableArray alloc] initWithCapacity:stringArray.count]; @@ -466,6 +437,31 @@ - (NSMutableArray*) makeTempViewsWithStrings:(NSArray *)stringArray andImages:(N return tempViewArray; } +- (NSMutableArray *)_makeLabelsWithStrings:(NSArray *)stringArray +{ + NSMutableArray *labelArray = [[NSMutableArray alloc] initWithCapacity:stringArray.count]; + + UIFont *font = kTextFont; + + for (NSString *string in stringArray) { + CGSize textSize = [string sizeWithFont:font]; + UIButton *textButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, textSize.width, textSize.height)]; + textButton.backgroundColor = [UIColor clearColor]; + textButton.titleLabel.font = font; + textButton.titleLabel.textAlignment = kTextAlignment; + textButton.titleLabel.textColor = kTextColor; + [textButton setTitle:string forState:UIControlStateNormal]; + textButton.layer.cornerRadius = 4.f; + [textButton setTitleColor:kTextColor forState:UIControlStateNormal]; + [textButton setTitleColor:kTextHighlightColor forState:UIControlStateHighlighted]; + [textButton addTarget:self action:@selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside]; + + [labelArray addObject:[textButton autorelease]]; + } + + return [labelArray autorelease]; +} + - (void)showAtPoint:(CGPoint)point inView:(UIView *)view withTitle:(NSString *)title withContentView:(UIView *)cView { [self showAtPoint:point inView:view withTitle:title withViewArray:[NSArray arrayWithObject:cView]]; From 5369c0e0fa51cf81fbb47e87671cb17908bbf291 Mon Sep 17 00:00:00 2001 From: Jared Allen Date: Thu, 31 Jan 2013 13:26:36 -0600 Subject: [PATCH 2/6] support for placing images (icons) to the left of strings Signed-off-by: Jared Allen --- PopoverView/PopoverView.m | 100 ++++++++++++++++++------ PopoverView/PopoverView_Configuration.h | 3 + 2 files changed, 78 insertions(+), 25 deletions(-) diff --git a/PopoverView/PopoverView.m b/PopoverView/PopoverView.m index 515e660..511c269 100644 --- a/PopoverView/PopoverView.m +++ b/PopoverView/PopoverView.m @@ -16,8 +16,14 @@ @interface PopoverView() - (NSMutableArray *)_makeTempViewsWithStrings:(NSArray *)stringArray andImages:(NSArray *)imageArray; -// Resturns a mutable array for UILabels generated from a NSArray of NSStrings -- (NSMutableArray *)_makeLabelsWithStrings:(NSArray *)stringArray; +// Returns an array of UIButtons generated from an array of NSStrings +- (NSMutableArray *)_makeButtonsWithStrings:(NSArray *)strings; + +// Returns an array of UIButtons with inline images generated from an array of NSStrings and UIImages +- (NSMutableArray *)_makeButtonsWithStrings:(NSArray *)strings andImages:(NSArray *)images; + +// generates a UIButton from a string +- (UIButton *)_makeButtonWithString:(NSString *)string; @end @@ -223,7 +229,7 @@ - (void)showAtPoint:(CGPoint)point inView:(UIView *)view withViewArray:(NSArray float padding = (i == viewArray.count-1) ? 0 : kBoxPadding; totalHeight += view.frame.size.height + padding; - + if (view.frame.size.width > totalWidth) { totalWidth = view.frame.size.width; } @@ -362,12 +368,12 @@ - (void)showAtPoint:(CGPoint)point inView:(UIView *)view withTitle:(NSString *)t - (void)showAtPoint:(CGPoint)point inView:(UIView *)view withStringArray:(NSArray *)stringArray { - [self showAtPoint:point inView:view withViewArray:[self _makeLabelsWithStrings:stringArray]]; + [self showAtPoint:point inView:view withViewArray:[self _makeButtonsWithStrings:stringArray]]; } - (void)showAtPoint:(CGPoint)point inView:(UIView *)view withTitle:(NSString *)title withStringArray:(NSArray *)stringArray { - [self showAtPoint:point inView:view withTitle:title withViewArray:[self _makeLabelsWithStrings:stringArray]]; + [self showAtPoint:point inView:view withTitle:title withViewArray:[self _makeButtonsWithStrings:stringArray]]; } - (void)showAtPoint:(CGPoint)point inView:(UIView *)view withStringArray:(NSArray *)stringArray withImageArray:(NSArray *)imageArray @@ -375,10 +381,11 @@ - (void)showAtPoint:(CGPoint)point inView:(UIView *)view withStringArray:(NSArra //Here we do something pretty similar to the stringArray method above. //We create an array of subviews that contains the strings and images centered above a label. - NSAssert((stringArray.count == imageArray.count), @"stringArray.count should equal imageArray.count"); - NSMutableArray* tempViewArray = [self _makeTempViewsWithStrings:stringArray andImages:imageArray]; - [self showAtPoint:point inView:view withViewArray:[tempViewArray autorelease]]; +// NSMutableArray* tempViewArray = [self _makeTempViewsWithStrings:stringArray andImages:imageArray]; + NSMutableArray *tempViews = [self _makeButtonsWithStrings:stringArray andImages:imageArray]; + + [self showAtPoint:point inView:view withViewArray:tempViews]; } - (void)showAtPoint:(CGPoint)point inView:(UIView *)view withTitle:(NSString *)title withStringArray:(NSArray *)stringArray withImageArray:(NSArray *)imageArray @@ -437,29 +444,72 @@ - (NSMutableArray *)_makeTempViewsWithStrings:(NSArray *)stringArray andImages:( return tempViewArray; } -- (NSMutableArray *)_makeLabelsWithStrings:(NSArray *)stringArray +- (NSMutableArray *)_makeButtonsWithStrings:(NSArray *)strings { - NSMutableArray *labelArray = [[NSMutableArray alloc] initWithCapacity:stringArray.count]; + NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:strings.count]; - UIFont *font = kTextFont; + for (NSString *string in strings) { + [buttons addObject:[self _makeButtonWithString:string]]; + } + + return [buttons autorelease]; +} + +- (NSMutableArray *)_makeButtonsWithStrings:(NSArray *)strings andImages:(NSArray *)images +{ + NSAssert((strings.count == images.count), @"strings.count should equal images.count"); + NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:strings.count]; + CGFloat maxImageWidth = 0.0; - for (NSString *string in stringArray) { - CGSize textSize = [string sizeWithFont:font]; - UIButton *textButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, textSize.width, textSize.height)]; - textButton.backgroundColor = [UIColor clearColor]; - textButton.titleLabel.font = font; - textButton.titleLabel.textAlignment = kTextAlignment; - textButton.titleLabel.textColor = kTextColor; - [textButton setTitle:string forState:UIControlStateNormal]; - textButton.layer.cornerRadius = 4.f; - [textButton setTitleColor:kTextColor forState:UIControlStateNormal]; - [textButton setTitleColor:kTextHighlightColor forState:UIControlStateHighlighted]; - [textButton addTarget:self action:@selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside]; + // create buttons and add images to them + for (int i = 0; i < strings.count; i++) { + UIButton *button = [self _makeButtonWithString:strings[i]]; - [labelArray addObject:[textButton autorelease]]; + // resize the button based on the image width + UIImage *image = images[i]; + button.frame = CGRectInset(button.frame, (image.size.width + kImageRightPadding) * -0.5, (kImageTopPadding + kImageBottomPadding) * -1.f); + + [button setImage:image forState:UIControlStateNormal]; + button.imageView.contentMode = UIViewContentModeScaleAspectFit; + [buttons addObject:button]; + + maxImageWidth = MAX(image.size.width, maxImageWidth); + } + + // resize buttons based on maxImageWidth + for (UIButton * button in buttons) { + CGFloat insetWidth = (maxImageWidth - button.imageView.frame.size.width); + button.frame = CGRectInset(button.frame, insetWidth * -0.25, 0); + button.imageEdgeInsets = UIEdgeInsetsMake(0, insetWidth * 0.5, 0, 0); + button.titleEdgeInsets = UIEdgeInsetsMake(0, insetWidth + kImageRightPadding, 0, 0); + } + + return [buttons autorelease]; +} + +- (UIButton *)_makeButtonWithString:(NSString *)string +{ + UIFont *font = kTextFont; + + CGSize textSize = [string sizeWithFont:font]; + UIButton *textButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, textSize.width, textSize.height)]; + textButton.autoresizingMask = UIViewAutoresizingFlexibleWidth; + textButton.backgroundColor = [UIColor clearColor]; + textButton.titleLabel.font = font; + textButton.titleLabel.textColor = kTextColor; + [textButton setTitle:string forState:UIControlStateNormal]; + textButton.layer.cornerRadius = 4.f; + [textButton setTitleColor:kTextColor forState:UIControlStateNormal]; + [textButton setTitleColor:kTextHighlightColor forState:UIControlStateHighlighted]; + [textButton addTarget:self action:@selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside]; + + if (kTextAlignment == UITextAlignmentLeft || kTextAlignment == NSTextAlignmentLeft) { + textButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; + } else if (kTextAlignment == UITextAlignmentRight || kTextAlignment == NSTextAlignmentRight) { + textButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight; } - return [labelArray autorelease]; + return [textButton autorelease]; } - (void)showAtPoint:(CGPoint)point inView:(UIView *)view withTitle:(NSString *)title withContentView:(UIView *)cView diff --git a/PopoverView/PopoverView_Configuration.h b/PopoverView/PopoverView_Configuration.h index 0c00fd8..3fb2db8 100644 --- a/PopoverView/PopoverView_Configuration.h +++ b/PopoverView/PopoverView_Configuration.h @@ -49,6 +49,9 @@ //padding along bottom of icons/images #define kImageBottomPadding 3.f +// padding between icons/images +#define kImageRightPadding 6.f + // DIVIDERS BETWEEN VIEWS From b0cd235cc5263122963402a0a47c3a5e26b61f18 Mon Sep 17 00:00:00 2001 From: Jared Allen Date: Thu, 31 Jan 2013 13:27:27 -0600 Subject: [PATCH 3/6] added method to create UIButtons with images above the title label - added kImageAboveLabel config to set if images should be display above or too the left of titleLabels. - added kImageTitlePadding config to set the padding between images and titleLabels in either configuration mentioned above. Signed-off-by: Jared Allen --- PopoverView/PopoverView.m | 119 ++++++++++++------------ PopoverView/PopoverView_Configuration.h | 8 +- 2 files changed, 64 insertions(+), 63 deletions(-) diff --git a/PopoverView/PopoverView.m b/PopoverView/PopoverView.m index 511c269..6eb9840 100644 --- a/PopoverView/PopoverView.m +++ b/PopoverView/PopoverView.m @@ -19,8 +19,11 @@ - (NSMutableArray *)_makeTempViewsWithStrings:(NSArray *)stringArray andImages:( // Returns an array of UIButtons generated from an array of NSStrings - (NSMutableArray *)_makeButtonsWithStrings:(NSArray *)strings; +// Returns an array of UIButtons with images above the label generated from an array of NSStrings and UIImages +- (NSMutableArray *)_makeVerticalButtonsWithStrings:(NSArray *)strings andImages:(NSArray *)images; + // Returns an array of UIButtons with inline images generated from an array of NSStrings and UIImages -- (NSMutableArray *)_makeButtonsWithStrings:(NSArray *)strings andImages:(NSArray *)images; +- (NSMutableArray *)_makeHorizontalButtonsWithStrings:(NSArray *)strings andImages:(NSArray *)images; // generates a UIButton from a string - (UIButton *)_makeButtonWithString:(NSString *)string; @@ -378,70 +381,16 @@ - (void)showAtPoint:(CGPoint)point inView:(UIView *)view withTitle:(NSString *)t - (void)showAtPoint:(CGPoint)point inView:(UIView *)view withStringArray:(NSArray *)stringArray withImageArray:(NSArray *)imageArray { - //Here we do something pretty similar to the stringArray method above. - //We create an array of subviews that contains the strings and images centered above a label. - - -// NSMutableArray* tempViewArray = [self _makeTempViewsWithStrings:stringArray andImages:imageArray]; - NSMutableArray *tempViews = [self _makeButtonsWithStrings:stringArray andImages:imageArray]; + NSMutableArray* tempViews = [self _makeButtonsWithStrings:stringArray andImages:imageArray]; [self showAtPoint:point inView:view withViewArray:tempViews]; } - (void)showAtPoint:(CGPoint)point inView:(UIView *)view withTitle:(NSString *)title withStringArray:(NSArray *)stringArray withImageArray:(NSArray *)imageArray { - NSAssert((stringArray.count == imageArray.count), @"stringArray.count should equal imageArray.count"); - NSMutableArray* tempViewArray = [self _makeTempViewsWithStrings:stringArray andImages:imageArray]; - - [self showAtPoint:point inView:view withTitle:title withViewArray:[tempViewArray autorelease]]; -} - -- (NSMutableArray *)_makeTempViewsWithStrings:(NSArray *)stringArray andImages:(NSArray *)imageArray -{ - NSMutableArray *tempViewArray = [[NSMutableArray alloc] initWithCapacity:stringArray.count]; - - UIFont *font = kTextFont; - - for (int i = 0; i < stringArray.count; i++) { - NSString *string = [stringArray objectAtIndex:i]; - - //First we build a label for the text to set in. - CGSize textSize = [string sizeWithFont:font]; - UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, textSize.width, textSize.height)]; - label.backgroundColor = [UIColor clearColor]; - label.font = font; - label.textAlignment = kTextAlignment; - label.textColor = kTextColor; - label.text = string; - label.layer.cornerRadius = 4.f; - - //Now we grab the image at the same index in the imageArray, and create - //a UIImageView for it. - UIImage *image = [imageArray objectAtIndex:i]; - UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; - - //Take the larger of the two widths as the width for the container - float containerWidth = MAX(imageView.frame.size.width, label.frame.size.width); - float containerHeight = label.frame.size.height + kImageTopPadding + kImageBottomPadding + imageView.frame.size.height; - - //This container will hold both the image and the label - UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, containerWidth, containerHeight)]; - - //Now we do the frame manipulations to put the imageView on top of the label, both centered - imageView.frame = CGRectMake(floorf(containerWidth*0.5f - imageView.frame.size.width*0.5f), kImageTopPadding, imageView.frame.size.width, imageView.frame.size.height); - label.frame = CGRectMake(floorf(containerWidth*0.5f - label.frame.size.width*0.5f), imageView.frame.size.height + kImageBottomPadding + kImageTopPadding, label.frame.size.width, label.frame.size.height); - - [containerView addSubview:imageView]; - [containerView addSubview:label]; - - [label release]; - [imageView release]; - - [tempViewArray addObject:containerView]; - [containerView release]; - } - - return tempViewArray; + NSMutableArray* tempViews = [self _makeButtonsWithStrings:stringArray andImages:imageArray]; + + [self showAtPoint:point inView:view withTitle:title withViewArray:tempViews]; } - (NSMutableArray *)_makeButtonsWithStrings:(NSArray *)strings @@ -456,6 +405,54 @@ - (NSMutableArray *)_makeButtonsWithStrings:(NSArray *)strings } - (NSMutableArray *)_makeButtonsWithStrings:(NSArray *)strings andImages:(NSArray *)images +{ + NSAssert((strings.count == images.count), @"strings.count should equal images.count"); + if (kImageAboveLabel) { + return [self _makeVerticalButtonsWithStrings:strings andImages:images]; + } else { + return [self _makeHorizontalButtonsWithStrings:strings andImages:images]; + } +} + +- (NSMutableArray *)_makeVerticalButtonsWithStrings:(NSArray *)strings andImages:(NSArray *)images +{ + NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:strings.count]; + + // create buttons and add images to them + for (int i = 0; i < strings.count; i++) { + UIButton *button = [self _makeButtonWithString:strings[i]]; + UIImage *image = images[i]; + [button setImage:image forState:UIControlStateNormal]; + button.imageView.contentMode = UIViewContentModeScaleAspectFit; + + //Take the larger of the two widths as the width for the button + float containerWidth = MAX(image.size.width, button.frame.size.width); + float containerHeight = button.frame.size.height + kImageTopPadding + kImageBottomPadding + kImageTitlePadding + image.size.height; + button.frame = CGRectMake(0, 0, containerWidth, containerHeight); + + [button setImage:image forState:UIControlStateNormal]; + button.imageView.contentMode = UIViewContentModeScaleAspectFit; + + CGSize imageSize = button.imageView.frame.size; + CGSize titleSize = button.titleLabel.frame.size; + + // lower the text and push it left to center it + button.titleEdgeInsets = UIEdgeInsetsMake(0.0, -imageSize.width, -(imageSize.height + kImageTitlePadding), 0.0); + + // the text width might have changed (in case it was shortened before due to + // lack of space and isn't anymore now), so we get the frame size again + titleSize = button.titleLabel.frame.size; + + // raise the image and push it right to center it + button.imageEdgeInsets = UIEdgeInsetsMake(-(titleSize.height + kImageTitlePadding), 0.0, 0.0, -titleSize.width); + + [buttons addObject:button]; + } + + return [buttons autorelease]; +} + +- (NSMutableArray *)_makeHorizontalButtonsWithStrings:(NSArray *)strings andImages:(NSArray *)images { NSAssert((strings.count == images.count), @"strings.count should equal images.count"); NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:strings.count]; @@ -467,7 +464,7 @@ - (NSMutableArray *)_makeButtonsWithStrings:(NSArray *)strings andImages:(NSArra // resize the button based on the image width UIImage *image = images[i]; - button.frame = CGRectInset(button.frame, (image.size.width + kImageRightPadding) * -0.5, (kImageTopPadding + kImageBottomPadding) * -1.f); + button.frame = CGRectInset(button.frame, (image.size.width + kImageTitlePadding) * -0.5, (kImageTopPadding + kImageBottomPadding) * -1.f); [button setImage:image forState:UIControlStateNormal]; button.imageView.contentMode = UIViewContentModeScaleAspectFit; @@ -481,7 +478,7 @@ - (NSMutableArray *)_makeButtonsWithStrings:(NSArray *)strings andImages:(NSArra CGFloat insetWidth = (maxImageWidth - button.imageView.frame.size.width); button.frame = CGRectInset(button.frame, insetWidth * -0.25, 0); button.imageEdgeInsets = UIEdgeInsetsMake(0, insetWidth * 0.5, 0, 0); - button.titleEdgeInsets = UIEdgeInsetsMake(0, insetWidth + kImageRightPadding, 0, 0); + button.titleEdgeInsets = UIEdgeInsetsMake(0, insetWidth + kImageTitlePadding, 0, 0); } return [buttons autorelease]; diff --git a/PopoverView/PopoverView_Configuration.h b/PopoverView/PopoverView_Configuration.h index 3fb2db8..c7c7e2a 100644 --- a/PopoverView/PopoverView_Configuration.h +++ b/PopoverView/PopoverView_Configuration.h @@ -49,9 +49,13 @@ //padding along bottom of icons/images #define kImageBottomPadding 3.f -// padding between icons/images -#define kImageRightPadding 6.f +//padding between icons/images and titles +#define kImageTitlePadding 6.f +// POSITION OF IMAGES + +//Bool that positions icons/images above or to the left of labels +#define kImageAboveLabel YES // DIVIDERS BETWEEN VIEWS From bd8de55205a9c60dca2eef0c89f6a91882ac85de Mon Sep 17 00:00:00 2001 From: Jared Allen Date: Thu, 31 Jan 2013 13:28:02 -0600 Subject: [PATCH 4/6] fixed bug that was not giving enough room to the longest title Signed-off-by: Jared Allen --- PopoverView/PopoverView.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PopoverView/PopoverView.m b/PopoverView/PopoverView.m index 6eb9840..6ebe4fd 100644 --- a/PopoverView/PopoverView.m +++ b/PopoverView/PopoverView.m @@ -464,7 +464,7 @@ - (NSMutableArray *)_makeHorizontalButtonsWithStrings:(NSArray *)strings andImag // resize the button based on the image width UIImage *image = images[i]; - button.frame = CGRectInset(button.frame, (image.size.width + kImageTitlePadding) * -0.5, (kImageTopPadding + kImageBottomPadding) * -1.f); + button.frame = CGRectInset(button.frame, image.size.width * -0.5 - kImageTitlePadding, (kImageTopPadding + kImageBottomPadding) * -1.f); [button setImage:image forState:UIControlStateNormal]; button.imageView.contentMode = UIViewContentModeScaleAspectFit; From bfe1db9a51273beb09a508736cd50edbc22fc84c Mon Sep 17 00:00:00 2001 From: Jared Allen Date: Thu, 31 Jan 2013 15:06:19 -0600 Subject: [PATCH 5/6] fixed issue with setting autoresizingMask on UIButtons with only text in Popover with title. For some reason the buttons frames were being resized past the Popover bounds causing issues with text alignment. Now all UIButtons are resized to the full width of the Popover view and text alignment is handled by the UIButton. Signed-off-by: Jared Allen --- PopoverView/PopoverView.m | 41 ++++++++++++--------------------------- 1 file changed, 12 insertions(+), 29 deletions(-) diff --git a/PopoverView/PopoverView.m b/PopoverView/PopoverView.m index 6ebe4fd..9b8bc0f 100644 --- a/PopoverView/PopoverView.m +++ b/PopoverView/PopoverView.m @@ -253,17 +253,10 @@ - (void)showAtPoint:(CGPoint)point inView:(UIView *)view withViewArray:(NSArray totalHeight = 0; - //Now we actually change the frame element for each subview, and center the views horizontally. + //Now we actually change the frame element for each subview to make sure all views are the full width. for (UIView *view in viewArray) { - if ([view autoresizingMask] == UIViewAutoresizingFlexibleWidth) { - //Now make sure all flexible views are the full width - view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, totalWidth, view.frame.size.height); - } else { - //If the view is not flexible width, then we position it centered in the view - //without stretching it. - view.frame = CGRectMake(floorf(CGRectGetMinX(boxFrame) + totalWidth*0.5f - view.frame.size.width*0.5f), view.frame.origin.y, view.frame.size.width, view.frame.size.height); - } - + view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, totalWidth, view.frame.size.height); + //and if dividers are enabled, we record their position for the drawing methods if (kShowDividersBetweenViews && i != viewArray.count-1) { CGRect dividerRect = CGRectMake(view.frame.origin.x, floorf(view.frame.origin.y + view.frame.size.height + kBoxPadding*0.5f), view.frame.size.width, 0.5f); @@ -316,13 +309,10 @@ - (void)showAtPoint:(CGPoint)point inView:(UIView *)view withTitle:(NSString *)t float padding = (i == viewArray.count-1) ? 0.f : kBoxPadding; totalHeight += view.frame.size.height + padding; - - if (view.frame.size.width > totalWidth) { - totalWidth = view.frame.size.width; - } - - [container addSubview:view]; - + totalWidth = MAX(totalWidth, view.frame.size.width); + + [container addSubview:view]; + i++; } @@ -332,17 +322,11 @@ - (void)showAtPoint:(CGPoint)point inView:(UIView *)view withTitle:(NSString *)t } i = 0; - - for (UIView *view in viewArray) { - if ([view autoresizingMask] == UIViewAutoresizingFlexibleWidth) { - //Now make sure all flexible views are the full width - view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, totalWidth, view.frame.size.height); - } else { - //If the view is not flexible width, then we position it centered in the view - //without stretching it. - view.frame = CGRectMake(floorf(CGRectGetMinX(boxFrame) + totalWidth*0.5f - view.frame.size.width*0.5f), view.frame.origin.y, view.frame.size.width, view.frame.size.height); - } - + + //Now we actually change the frame element for each subview to make sure all views are the full width. + for (UIView *view in viewArray) { + view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, totalWidth, view.frame.size.height); + //and if dividers are enabled, we record their position for the drawing methods if (kShowDividersBetweenViews && i != viewArray.count-1) { CGRect dividerRect = CGRectMake(view.frame.origin.x, floorf(view.frame.origin.y + view.frame.size.height + kBoxPadding*0.5f), view.frame.size.width, 0.5f); @@ -490,7 +474,6 @@ - (UIButton *)_makeButtonWithString:(NSString *)string CGSize textSize = [string sizeWithFont:font]; UIButton *textButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, textSize.width, textSize.height)]; - textButton.autoresizingMask = UIViewAutoresizingFlexibleWidth; textButton.backgroundColor = [UIColor clearColor]; textButton.titleLabel.font = font; textButton.titleLabel.textColor = kTextColor; From 37c7000b8814034539236d045c93dd9d3d201975 Mon Sep 17 00:00:00 2001 From: Jared Allen Date: Thu, 31 Jan 2013 15:07:20 -0600 Subject: [PATCH 6/6] removed declaration of unused method Signed-off-by: Jared Allen --- PopoverView/PopoverView.m | 2 -- 1 file changed, 2 deletions(-) diff --git a/PopoverView/PopoverView.m b/PopoverView/PopoverView.m index 9b8bc0f..644230d 100644 --- a/PopoverView/PopoverView.m +++ b/PopoverView/PopoverView.m @@ -14,8 +14,6 @@ @interface PopoverView() -- (NSMutableArray *)_makeTempViewsWithStrings:(NSArray *)stringArray andImages:(NSArray *)imageArray; - // Returns an array of UIButtons generated from an array of NSStrings - (NSMutableArray *)_makeButtonsWithStrings:(NSArray *)strings;