diff --git a/Classes/ios/MMPopLabel.h b/Classes/ios/MMPopLabel.h index 9626160..baf64f7 100644 --- a/Classes/ios/MMPopLabel.h +++ b/Classes/ios/MMPopLabel.h @@ -25,9 +25,19 @@ @property (nonatomic, weak) id delegate; ++ (MMPopLabel *)popLabelWithAttributedString:(NSAttributedString *)attributedString; + + (MMPopLabel *)popLabelWithText:(NSString *)text; - (void)addButton:(UIButton *)button; + +- (void)popAtView:(UIView *)view animated: (BOOL) animated; + +//Keeping this method without animated boolean for retrocompatibility +//will use animations by default - (void)popAtView:(UIView *)view; + +- (void)setText: (NSString *)text; + - (void)dismiss; diff --git a/Classes/ios/MMPopLabel.m b/Classes/ios/MMPopLabel.m index 5aa5769..71d0e0b 100644 --- a/Classes/ios/MMPopLabel.m +++ b/Classes/ios/MMPopLabel.m @@ -15,7 +15,7 @@ CGFloat const kMMPopLabelSidePadding = 10.0f; -CGFloat const kMMPopLabelViewPadding = 2.0f; +CGFloat const kMMPopLabelViewPadding = 8.0f; CGFloat const kMMPopLabelCornerRadius = 6.0f; CGFloat const kMMPopLabelTipPadding = 8.0f; @@ -51,14 +51,45 @@ @interface MMPopLabel () @implementation MMPopLabel ++ (MMPopLabel *)popLabelWithAttributedString:(NSAttributedString *)attributedString { + MMPopLabel *popLabel = [[MMPopLabel alloc] initWithAttributedString: attributedString]; + + + return popLabel; + +} + (MMPopLabel *)popLabelWithText:(NSString *)text { MMPopLabel *popLabel = [[MMPopLabel alloc] initWithText:text]; - + return popLabel; } +- (instancetype)initWithAttributedString: (NSAttributedString *)attributedString { + if (self = [super initWithFrame:CGRectZero]) + { + self.buttons = [@[] mutableCopy]; + + self.backgroundColor = [UIColor clearColor]; + self.hidden = YES; + + if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { + self.tipSize = 24; + } else { + self.tipSize = 12; + } + + self.label = [[MMLabel alloc] initWithFrame:CGRectZero]; + self.label.textAlignment = NSTextAlignmentCenter; + self.label.attributedText = attributedString; + self.label.backgroundColor = [UIColor clearColor]; + self.label.numberOfLines = 0; + + [self addSubview:self.label]; + } + return self; +} - (id)initWithText:(NSString *)text { @@ -68,13 +99,13 @@ - (id)initWithText:(NSString *)text self.backgroundColor = [UIColor clearColor]; self.hidden = YES; - + if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { self.tipSize = 24; } else { self.tipSize = 12; } - + self.label = [[MMLabel alloc] initWithFrame:CGRectZero]; self.label.textAlignment = NSTextAlignmentCenter; self.label.text = text; @@ -86,6 +117,9 @@ - (id)initWithText:(NSString *)text return self; } +- (void)setText: (NSString *)text { + self.label.text = text; +} - (void)setupAppearance { @@ -108,11 +142,11 @@ - (void)setupAppearance self.label.textColor = _labelTextColor; self.label.font = _labelFont; - + /* resize label and view */ CGFloat maxWidth = [UIScreen mainScreen].applicationFrame.size.width * 0.80f; CGFloat maxHeight = [UIScreen mainScreen].applicationFrame.size.height * 0.80f; - + NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:self.label.text attributes:@{NSFontAttributeName:_labelFont}]; CGRect rect = [attributedText boundingRectWithSize:(CGSize){maxWidth, CGFLOAT_MAX} @@ -120,7 +154,7 @@ - (void)setupAppearance context:nil]; CGFloat minWidth = MAX(rect.size.width, 180); CGFloat minHeight = MIN(rect.size.height, maxHeight); - + self.label.frame = CGRectMake(0, 0, minWidth, minHeight); [self.label sizeToFit]; @@ -132,7 +166,7 @@ - (void)setupAppearance self.label.frame = CGRectMake(_targetFrame.origin.x, _targetFrame.origin.y, minWidth, _targetFrame.size.height + _tipSize * 4); - + /* add buttons, if any */ if (_buttons.count == 0) return; @@ -140,7 +174,7 @@ - (void)setupAppearance self.frame.origin.y, self.frame.size.width, self.frame.size.height + 33); - + NSInteger index = 0; NSInteger buttonWidth = self.frame.size.width / _buttons.count; for (UIButton *b in _buttons) { @@ -148,13 +182,13 @@ - (void)setupAppearance b.frame = CGRectMake(self.bounds.origin.x + (index * buttonWidth), self.bounds.origin.y + self.bounds.size.height - 44, buttonWidth, 33); - + if (_buttonFont) { b.titleLabel.font = _buttonFont; } else { b.titleLabel.font = [UIFont systemFontOfSize:_tipSize]; } - + [b setTitleColor:_labelTextColor forState:UIControlStateNormal]; [b setTitleColor:_labelTextHighlightColor forState:UIControlStateHighlighted]; @@ -177,13 +211,16 @@ - (void)layoutSubviews [self setupAppearance]; } +- (void)popAtView:(UIView *)view { + [self popAtView:view animated:YES]; +} -- (void)popAtView:(UIView *)view +- (void)popAtView:(UIView *)view animated: (BOOL) animated { if (self.hidden == NO) return; - + _arrowType = MMPopLabelTopArrow; - + CGPoint position = CGPointMake(view.center.x, view.center.y + view.frame.size.height / 2 + kMMPopLabelViewPadding); self.center = position; if (position.x + (self.frame.size.width / 2) > [UIScreen mainScreen].applicationFrame.size.width) { @@ -194,12 +231,13 @@ - (void)popAtView:(UIView *)view position = CGPointMake(view.center.x + diff, view.center.y + view.frame.size.height / 2); } - if (self.frame.origin.y + self.frame.size.height > [UIScreen mainScreen].applicationFrame.size.height) { + if (self.frame.origin.y + self.frame.size.height > [[self superview] frame].size.height) { + _arrowType = MMPopLabelBottomArrow; position = CGPointMake(position.x, - [UIScreen mainScreen].applicationFrame.size.height - (self.frame.size.height + view.frame.size.height + kMMPopLabelViewPadding)); + [[self superview] frame].size.height - (self.frame.size.height + view.frame.size.height + kMMPopLabelViewPadding)); } - + CGPoint centerPoint = CGPointMake(position.x, position.y + self.frame.size.height / 2); self.center = position; @@ -212,39 +250,45 @@ - (void)popAtView:(UIView *)view _viewCenter = CGPointMake(view.center.x - self.frame.origin.x - 8, view.center.y); [self setNeedsDisplay]; - self.transform = CGAffineTransformMakeScale(0, 0); - view.transform = CGAffineTransformMakeScale(0, 0); - [UIView animateKeyframesWithDuration:duration/6.0f delay:delay options:0 animations:^{ - self.center = centerPoint; - self.alpha = 1.0f; - self.transform = CGAffineTransformMakeScale(1.2, 1.2); - view.transform = CGAffineTransformMakeScale(1.2, 1.2); - } completion:^(BOOL finished) { - [UIView animateKeyframesWithDuration:duration/6.0f delay:0 options:0 animations:^{ - self.transform = CGAffineTransformMakeScale(0.9, 0.9); - view.transform = CGAffineTransformMakeScale(0.9, 0.9); + if (animated) { + self.transform = CGAffineTransformMakeScale(0, 0); + view.transform = CGAffineTransformMakeScale(0, 0); + + [UIView animateKeyframesWithDuration:duration/6.0f delay:delay options:0 animations:^{ + self.center = centerPoint; + self.alpha = 1.0f; + self.transform = CGAffineTransformMakeScale(1.2, 1.2); + view.transform = CGAffineTransformMakeScale(1.2, 1.2); } completion:^(BOOL finished) { [UIView animateKeyframesWithDuration:duration/6.0f delay:0 options:0 animations:^{ - self.transform = CGAffineTransformMakeScale(1, 1); - view.transform = CGAffineTransformMakeScale(1, 1); + self.transform = CGAffineTransformMakeScale(0.9, 0.9); + view.transform = CGAffineTransformMakeScale(0.9, 0.9); } completion:^(BOOL finished) { - // completion block empty? + [UIView animateKeyframesWithDuration:duration/6.0f delay:0 options:0 animations:^{ + self.transform = CGAffineTransformMakeScale(1, 1); + view.transform = CGAffineTransformMakeScale(1, 1); + } completion:^(BOOL finished) { + // completion block empty? + }]; }]; }]; - }]; + } else { + self.center = centerPoint; + self.alpha = 1.0f; + } } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { - [self dismiss]; + } - (void)dismiss { if (self.hidden == YES) return; - + [UIView animateWithDuration:0.15f animations:^{ self.alpha = 0.0f; } completion:^(BOOL finished) { @@ -268,10 +312,15 @@ - (void)drawRect:(CGRect)rect } CGContextRotateCTM(context, -45 * M_PI / 180); - UIBezierPath* tipPath = [UIBezierPath bezierPathWithRect: CGRectMake(0, 0, 11, 11)]; + UIBezierPath *tipPath = [UIBezierPath bezierPath]; + [tipPath moveToPoint:CGPointMake( 0, 0)]; + [tipPath addLineToPoint:CGPointMake(0, 11)]; + [tipPath addLineToPoint:CGPointMake(11, 11)]; + [tipPath addLineToPoint:CGPointMake( 0, 0)]; + [tipPath closePath]; + [_labelColor setFill]; [tipPath fill]; - CGContextRestoreGState(context); //// ViewBackground Drawing diff --git a/MMPopLabel.podspec b/MMPopLabel.podspec index d264ac9..777c72a 100644 --- a/MMPopLabel.podspec +++ b/MMPopLabel.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "MMPopLabel" - s.version = "0.1.1" + s.version = "0.1.9" s.summary = "MMPopLabel is a tooltip control for iOS, with optional buttons" s.description = <<-DESC MMPopLabel is a tooltip control for iOS, useful for tutorials.