From f2ee11b50e656a56906cb49b0423b16198bb98f9 Mon Sep 17 00:00:00 2001 From: Yichi Zhang Date: Mon, 9 Feb 2015 22:58:13 +1100 Subject: [PATCH 1/4] Adding accessibilityHint when initializing with title and message --- CMPopTipView/CMPopTipView.m | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/CMPopTipView/CMPopTipView.m b/CMPopTipView/CMPopTipView.m index 3018fcd..88d1d53 100644 --- a/CMPopTipView/CMPopTipView.m +++ b/CMPopTipView/CMPopTipView.m @@ -699,7 +699,12 @@ - (id)initWithFrame:(CGRect)frame self.textFont = [UIFont boldSystemFontOfSize:14.0]; self.textColor = [UIColor whiteColor]; - self.textAlignment = NSTextAlignmentCenter; + self.textAlignment = NSTextAlignmentCenter; + + self.titleFont = [UIFont boldSystemFontOfSize:16.0]; + self.titleColor = [UIColor whiteColor]; + self.titleAlignment = NSTextAlignmentCenter; + self.backgroundColor = [UIColor colorWithRed:62.0/255.0 green:60.0/255.0 blue:154.0/255.0 alpha:1.0]; self.has3DStyle = YES; self.borderColor = [UIColor blackColor]; @@ -739,28 +744,28 @@ - (id)initWithTitle:(NSString *)titleToShow message:(NSString *)messageToShow CGRect frame = CGRectZero; if ((self = [self initWithFrame:frame])) { + self.title = titleToShow; - self.message = messageToShow; + self.message = messageToShow; - self.titleFont = [UIFont boldSystemFontOfSize:16.0]; - self.titleColor = [UIColor whiteColor]; - self.titleAlignment = NSTextAlignmentCenter; - self.textFont = [UIFont systemFontOfSize:14.0]; - self.textColor = [UIColor whiteColor]; + self.isAccessibilityElement = YES; + self.accessibilityHint = messageToShow; } return self; } - (id)initWithMessage:(NSString *)messageToShow { - CGRect frame = CGRectZero; - - if ((self = [self initWithFrame:frame])) { - self.message = messageToShow; + CGRect frame = CGRectZero; + + if ((self = [self initWithFrame:frame])) { + + self.message = messageToShow; + self.isAccessibilityElement = YES; self.accessibilityHint = messageToShow; - } - return self; + } + return self; } - (id)initWithCustomView:(UIView *)aView From 293001c027f5e38f94cc03ea00e82ef4fe7a6e9c Mon Sep 17 00:00:00 2001 From: Yichi Zhang Date: Mon, 9 Feb 2015 23:07:24 +1100 Subject: [PATCH 2/4] Organized methods in implementation file --- CMPopTipView/CMPopTipView.m | 161 +++++++++++++++++++----------------- 1 file changed, 84 insertions(+), 77 deletions(-) diff --git a/CMPopTipView/CMPopTipView.m b/CMPopTipView/CMPopTipView.m index 88d1d53..4468342 100644 --- a/CMPopTipView/CMPopTipView.m +++ b/CMPopTipView/CMPopTipView.m @@ -44,6 +44,80 @@ @interface CMPopTipView () @implementation CMPopTipView +#pragma mark - Init Methods +- (id)initWithFrame:(CGRect)frame +{ + if ((self = [super initWithFrame:frame])) { + // Initialization code + self.opaque = NO; + + _topMargin = 2.0; + _pointerSize = 12.0; + _sidePadding = 2.0; + _borderWidth = 1.0; + + self.textFont = [UIFont boldSystemFontOfSize:14.0]; + self.textColor = [UIColor whiteColor]; + self.textAlignment = NSTextAlignmentCenter; + + self.titleFont = [UIFont boldSystemFontOfSize:16.0]; + self.titleColor = [UIColor whiteColor]; + self.titleAlignment = NSTextAlignmentCenter; + + self.backgroundColor = [UIColor colorWithRed:62.0/255.0 green:60.0/255.0 blue:154.0/255.0 alpha:1.0]; + self.has3DStyle = YES; + self.borderColor = [UIColor blackColor]; + self.hasShadow = YES; + self.animation = CMPopTipAnimationSlide; + self.dismissTapAnywhere = NO; + self.preferredPointDirection = PointDirectionAny; + self.hasGradientBackground = YES; + self.cornerRadius = 10.0; + } + return self; +} + +- (id)initWithTitle:(NSString *)titleToShow message:(NSString *)messageToShow +{ + CGRect frame = CGRectZero; + + if ((self = [self initWithFrame:frame])) { + + self.title = titleToShow; + self.message = messageToShow; + + self.isAccessibilityElement = YES; + self.accessibilityHint = messageToShow; + } + return self; +} + +- (id)initWithMessage:(NSString *)messageToShow +{ + CGRect frame = CGRectZero; + + if ((self = [self initWithFrame:frame])) { + + self.message = messageToShow; + + self.isAccessibilityElement = YES; + self.accessibilityHint = messageToShow; + } + return self; +} + +- (id)initWithCustomView:(UIView *)aView +{ + CGRect frame = CGRectZero; + + if ((self = [self initWithFrame:frame])) { + self.customView = aView; + [self addSubview:self.customView]; + } + return self; +} + +#pragma mark Size calculation - (CGRect)bubbleFrame { CGRect bubbleFrame; if (_pointDirection == PointDirectionUp) { @@ -64,6 +138,7 @@ - (CGRect)contentFrame { return contentFrame; } +#pragma mark Layout and draw - (void)layoutSubviews { if (self.customView) { @@ -346,6 +421,7 @@ - (void)drawRect:(__unused CGRect)rect } } +#pragma mark Presentation - (void)presentPointingAtView:(UIView *)targetView inView:(UIView *)containerView animated:(BOOL)animated { if (!self.targetObject) { self.targetObject = targetView; @@ -595,6 +671,7 @@ - (void)presentPointingAtBarButtonItem:(UIBarButtonItem *)barButtonItem animated [self presentPointingAtView:targetView inView:containerView animated:animated]; } +#pragma mark Dismiss - (void)finaliseDismiss { [self.autoDismissTimer invalidate]; self.autoDismissTimer = nil; @@ -609,11 +686,6 @@ - (void)finaliseDismiss { self.targetObject = nil; } -- (void)dismissAnimationDidStop:(__unused NSString *)animationID finished:(__unused NSNumber *)finished context:(__unused void *)context -{ - [self finaliseDismiss]; -} - - (void)dismissAnimated:(BOOL)animated { if (animated) { @@ -677,6 +749,12 @@ - (void)dismissByUser [self notifyDelegatePopTipViewWasDismissedByUser]; } +#pragma mark Animation call back +- (void)dismissAnimationDidStop:(__unused NSString *)animationID finished:(__unused NSNumber *)finished context:(__unused void *)context +{ + [self finaliseDismiss]; +} + - (void)popAnimationDidStop:(__unused NSString *)animationID finished:(__unused NSNumber *)finished context:(__unused void *)context { // at the end set to normal size @@ -686,38 +764,7 @@ - (void)popAnimationDidStop:(__unused NSString *)animationID finished:(__unused [UIView commitAnimations]; } -- (id)initWithFrame:(CGRect)frame -{ - if ((self = [super initWithFrame:frame])) { - // Initialization code - self.opaque = NO; - - _topMargin = 2.0; - _pointerSize = 12.0; - _sidePadding = 2.0; - _borderWidth = 1.0; - - self.textFont = [UIFont boldSystemFontOfSize:14.0]; - self.textColor = [UIColor whiteColor]; - self.textAlignment = NSTextAlignmentCenter; - - self.titleFont = [UIFont boldSystemFontOfSize:16.0]; - self.titleColor = [UIColor whiteColor]; - self.titleAlignment = NSTextAlignmentCenter; - - self.backgroundColor = [UIColor colorWithRed:62.0/255.0 green:60.0/255.0 blue:154.0/255.0 alpha:1.0]; - self.has3DStyle = YES; - self.borderColor = [UIColor blackColor]; - self.hasShadow = YES; - self.animation = CMPopTipAnimationSlide; - self.dismissTapAnywhere = NO; - self.preferredPointDirection = PointDirectionAny; - self.hasGradientBackground = YES; - self.cornerRadius = 10.0; - } - return self; -} - +#pragma mark Other - (void)setHasShadow:(BOOL)hasShadow { if (hasShadow != _hasShadow) { @@ -739,44 +786,4 @@ - (PointDirection) getPointDirection return _pointDirection; } -- (id)initWithTitle:(NSString *)titleToShow message:(NSString *)messageToShow -{ - CGRect frame = CGRectZero; - - if ((self = [self initWithFrame:frame])) { - - self.title = titleToShow; - self.message = messageToShow; - - self.isAccessibilityElement = YES; - self.accessibilityHint = messageToShow; - } - return self; -} - -- (id)initWithMessage:(NSString *)messageToShow -{ - CGRect frame = CGRectZero; - - if ((self = [self initWithFrame:frame])) { - - self.message = messageToShow; - - self.isAccessibilityElement = YES; - self.accessibilityHint = messageToShow; - } - return self; -} - -- (id)initWithCustomView:(UIView *)aView -{ - CGRect frame = CGRectZero; - - if ((self = [self initWithFrame:frame])) { - self.customView = aView; - [self addSubview:self.customView]; - } - return self; -} - @end From 0b6124987ee286119b4ff28b9f8cb1fe617fc874 Mon Sep 17 00:00:00 2001 From: Yichi Zhang Date: Mon, 9 Feb 2015 23:28:07 +1100 Subject: [PATCH 3/4] There is a bit of repeating when calculating the bounding size of message and title. Put them in two separate methods. --- CMPopTipView/CMPopTipView.m | 166 +++++++++++++++++------------------- 1 file changed, 79 insertions(+), 87 deletions(-) diff --git a/CMPopTipView/CMPopTipView.m b/CMPopTipView/CMPopTipView.m index 4468342..0b5cb7e 100644 --- a/CMPopTipView/CMPopTipView.m +++ b/CMPopTipView/CMPopTipView.m @@ -138,6 +138,70 @@ - (CGRect)contentFrame { return contentFrame; } +- (CGSize)titleBoundingSizeWithWidth:(CGFloat)width { + CGSize titleSize = CGSizeZero; + if (self.title) { + + if ([self.title respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) { + NSMutableParagraphStyle *titleParagraphStyle = [[NSMutableParagraphStyle alloc] init]; + titleParagraphStyle.lineBreakMode = NSLineBreakByWordWrapping; + + titleSize = [self.title boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) + options:NSStringDrawingUsesLineFragmentOrigin + attributes:@{ + NSFontAttributeName: self.titleFont, + NSParagraphStyleAttributeName: titleParagraphStyle + } + context:nil].size; + } + else { + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + + titleSize = [self.title sizeWithFont:self.titleFont + constrainedToSize:CGSizeMake(width, CGFLOAT_MAX) + lineBreakMode:NSLineBreakByWordWrapping]; + +#pragma clang diagnostic pop + + } + } + return titleSize; +} + +- (CGSize)messageBoundingSizeWithWidth:(CGFloat)width { + CGSize textSize = CGSizeZero; + if (self.message) { + if ([self.message respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) { + NSMutableParagraphStyle *textParagraphStyle = [[NSMutableParagraphStyle alloc] init]; + textParagraphStyle.alignment = self.textAlignment; + textParagraphStyle.lineBreakMode =NSLineBreakByWordWrapping; + + textSize = [self.message boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) + options:NSStringDrawingUsesLineFragmentOrigin + attributes:@{ + NSFontAttributeName: self.textFont, + NSParagraphStyleAttributeName: textParagraphStyle + } + context:nil].size; + } + else { + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + + textSize = [self.message sizeWithFont:self.textFont + constrainedToSize:CGSizeMake(width, CGFLOAT_MAX) + lineBreakMode:NSLineBreakByWordWrapping]; + +#pragma clang diagnostic pop + + } + } + return textSize; +} + #pragma mark Layout and draw - (void)layoutSubviews { if (self.customView) { @@ -366,30 +430,8 @@ - (void)drawRect:(__unused CGRect)rect // Move down to make room for title if (self.title) { - if ([self.title respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) { - NSMutableParagraphStyle *titleParagraphStyle = [[NSMutableParagraphStyle alloc] init]; - titleParagraphStyle.lineBreakMode = NSLineBreakByWordWrapping; - - textFrame.origin.y += [self.title boundingRectWithSize:CGSizeMake(textFrame.size.width, 99999.0) - options:NSStringDrawingUsesLineFragmentOrigin - attributes:@{ - NSFontAttributeName: self.titleFont, - NSParagraphStyleAttributeName: titleParagraphStyle - } - context:nil].size.height; - } - else { - -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - - textFrame.origin.y += [self.title sizeWithFont:self.titleFont - constrainedToSize:CGSizeMake(textFrame.size.width, 99999.0) - lineBreakMode:NSLineBreakByWordWrapping].height; - -#pragma clang diagnostic pop - - } + CGSize titleBoundingSize = [self titleBoundingSizeWithWidth:textFrame.size.width]; + textFrame.origin.y += titleBoundingSize.height; } NSMutableParagraphStyle *textParagraphStyle = [[NSMutableParagraphStyle alloc] init]; @@ -398,11 +440,12 @@ - (void)drawRect:(__unused CGRect)rect if ([self.message respondsToSelector:@selector(drawWithRect:options:attributes:context:)]) { [self.message drawWithRect:textFrame - options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ - NSFontAttributeName: self.textFont, - NSParagraphStyleAttributeName: textParagraphStyle, - NSForegroundColorAttributeName: self.textColor - } + options:NSStringDrawingUsesLineFragmentOrigin + attributes:@{ + NSFontAttributeName: self.textFont, + NSParagraphStyleAttributeName: textParagraphStyle, + NSForegroundColorAttributeName: self.textColor + } context:nil]; } else { @@ -471,69 +514,18 @@ - (void)presentPointingAtView:(UIView *)targetView inView:(UIView *)containerVie } } - CGSize textSize = CGSizeZero; + CGSize textSize = [self messageBoundingSizeWithWidth:rectWidth]; - if (self.message!=nil) { - if ([self.message respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) { - NSMutableParagraphStyle *textParagraphStyle = [[NSMutableParagraphStyle alloc] init]; - textParagraphStyle.alignment = self.textAlignment; - textParagraphStyle.lineBreakMode =NSLineBreakByWordWrapping; - - textSize = [self.message boundingRectWithSize:CGSizeMake(rectWidth, 99999.0) - options:NSStringDrawingUsesLineFragmentOrigin - attributes:@{ - NSFontAttributeName: self.textFont, - NSParagraphStyleAttributeName: textParagraphStyle - } - context:nil].size; - } - else { - -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - - textSize = [self.message sizeWithFont:self.textFont - constrainedToSize:CGSizeMake(rectWidth, 99999.0) - lineBreakMode:NSLineBreakByWordWrapping]; - -#pragma clang diagnostic pop - - } - } if (self.customView != nil) { textSize = self.customView.frame.size; } - if (self.title != nil) { - CGSize titleSize; - - if ([self.title respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) { - NSMutableParagraphStyle *titleParagraphStyle = [[NSMutableParagraphStyle alloc] init]; - titleParagraphStyle.lineBreakMode = NSLineBreakByWordWrapping; - - titleSize = [self.title boundingRectWithSize:CGSizeMake(rectWidth, 99999.0) - options:NSStringDrawingUsesLineFragmentOrigin - attributes:@{ - NSFontAttributeName: self.titleFont, - NSParagraphStyleAttributeName: titleParagraphStyle - } - context:nil].size; - } - else { - -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - - titleSize = [self.title sizeWithFont:self.titleFont - constrainedToSize:CGSizeMake(rectWidth, 99999.0) - lineBreakMode:NSLineBreakByWordWrapping]; - -#pragma clang diagnostic pop - - } - - if (titleSize.width > textSize.width) textSize.width = titleSize.width; - textSize.height += titleSize.height; + + CGSize titleSize = [self titleBoundingSizeWithWidth:rectWidth]; + + if (titleSize.width > textSize.width){ + textSize.width = titleSize.width; } + textSize.height += titleSize.height; _bubbleSize = CGSizeMake(textSize.width + _cornerRadius*2, textSize.height + _cornerRadius*2); From 33353f5acd1aecf1f7fc0364217c77c0d2b9a880 Mon Sep 17 00:00:00 2001 From: Yichi Zhang Date: Mon, 9 Feb 2015 23:33:21 +1100 Subject: [PATCH 4/4] Changed a spelling mistake. Changed 'colourHL' variable to 'colorHL'. --- CMPopTipView/CMPopTipView.m | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/CMPopTipView/CMPopTipView.m b/CMPopTipView/CMPopTipView.m index 0b5cb7e..f385b95 100644 --- a/CMPopTipView/CMPopTipView.m +++ b/CMPopTipView/CMPopTipView.m @@ -287,9 +287,9 @@ - (void)drawRect:(__unused CGRect)rect size_t locationCount = 5; CGFloat locationList[] = {0.0, bubbleMiddle-0.03, bubbleMiddle, bubbleMiddle+0.03, 1.0}; - CGFloat colourHL = 0.0; + CGFloat colorHL = 0.0; if (_highlight) { - colourHL = 0.25; + colorHL = 0.25; } CGFloat red; @@ -312,11 +312,11 @@ - (void)drawRect:(__unused CGRect)rect } CGFloat colorList[] = { //red, green, blue, alpha - red*1.16+colourHL, green*1.16+colourHL, blue*1.16+colourHL, alpha, - red*1.16+colourHL, green*1.16+colourHL, blue*1.16+colourHL, alpha, - red*1.08+colourHL, green*1.08+colourHL, blue*1.08+colourHL, alpha, - red +colourHL, green +colourHL, blue +colourHL, alpha, - red +colourHL, green +colourHL, blue +colourHL, alpha + red*1.16+colorHL, green*1.16+colorHL, blue*1.16+colorHL, alpha, + red*1.16+colorHL, green*1.16+colorHL, blue*1.16+colorHL, alpha, + red*1.08+colorHL, green*1.08+colorHL, blue*1.08+colorHL, alpha, + red +colorHL, green +colorHL, blue +colorHL, alpha, + red +colorHL, green +colorHL, blue +colorHL, alpha }; myColorSpace = CGColorSpaceCreateDeviceRGB(); myGradient = CGGradientCreateWithColorComponents(myColorSpace, colorList, locationList, locationCount); @@ -702,10 +702,10 @@ - (void)autoDismissAnimatedDidFire:(NSTimer *)theTimer { [self notifyDelegatePopTipViewWasDismissedByUser]; } -- (void)autoDismissAnimated:(BOOL)animated atTimeInterval:(NSTimeInterval)timeInvertal { +- (void)autoDismissAnimated:(BOOL)animated atTimeInterval:(NSTimeInterval)timeInterval { NSDictionary * userInfo = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:animated] forKey:@"animated"]; - self.autoDismissTimer = [NSTimer scheduledTimerWithTimeInterval:timeInvertal + self.autoDismissTimer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(autoDismissAnimatedDidFire:) userInfo:userInfo