From 560ea1030d606dd2d66884415bd12030f0f2bbcd Mon Sep 17 00:00:00 2001 From: panxi Date: Mon, 1 Apr 2019 12:01:44 -0400 Subject: [PATCH] add actions into mflabel --- MVMCoreUI/Atoms/Views/MFLabel.h | 2 ++ MVMCoreUI/Atoms/Views/MFLabel.m | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/MVMCoreUI/Atoms/Views/MFLabel.h b/MVMCoreUI/Atoms/Views/MFLabel.h index a5ca49ab..7a73e1df 100644 --- a/MVMCoreUI/Atoms/Views/MFLabel.h +++ b/MVMCoreUI/Atoms/Views/MFLabel.h @@ -20,6 +20,8 @@ // Set this to use a custom sizing object during updateView instead of the standard. @property (nonatomic, strong, nullable) MFSizeObject *sizeObject; +@property (nonatomic, strong, nullable) NSDictionary *actions; + // Set the font and set to scale - (void)setFont:(nonnull UIFont *)font scale:(BOOL)scale; diff --git a/MVMCoreUI/Atoms/Views/MFLabel.m b/MVMCoreUI/Atoms/Views/MFLabel.m index 0370e23f..0bf52d22 100644 --- a/MVMCoreUI/Atoms/Views/MFLabel.m +++ b/MVMCoreUI/Atoms/Views/MFLabel.m @@ -17,6 +17,8 @@ @import MVMCore.MVMCoreGetterUtility; @import MVMCore.NSDictionary_MFConvenience; @import MVMCore.MVMCoreJSONConstants; +@import MVMCore.MVMCoreActionHandler; +@import MVMCore.UILabel_MFCustom; @interface MFLabel () @@ -198,6 +200,7 @@ NSArray *attributes = [json array:@"attributes"]; if (attributes) { NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:label.text attributes:@{NSFontAttributeName:label.font,NSForegroundColorAttributeName:label.textColor}]; + NSMutableDictionary *actions = [NSMutableDictionary new]; for (NSDictionary *attribute in attributes) { NSNumber *location = [attribute optionalNumberForKey:@"location"]; NSNumber *length = [attribute optionalNumberForKey:@"length"]; @@ -225,10 +228,18 @@ if (font) { [attributedString addAttribute:NSFontAttributeName value:font range:range]; } + } else if ([type isEqualToString:@"link"]) { + label.userInteractionEnabled = YES; + [actions setObject:attribute forKey:[NSValue valueWithRange:range]]; } } } label.attributedText = attributedString; + if ([label isKindOfClass:[MFLabel class]]) { + MFLabel *mflabel = (MFLabel *)label; + mflabel.actions = actions; + } + } } } @@ -289,4 +300,25 @@ } +#pragma mark - action + +- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { + NSDictionary *actionMap = [self areTouchesInActionString:touches]; + if (actionMap) { + [[MVMCoreActionHandler sharedActionHandler] handleActionWithDictionary:actionMap additionalData:nil delegate:nil]; + } +} + +- (NSDictionary *)areTouchesInActionString:(NSSet *)touches { + CGPoint location = [[touches anyObject] locationInView:self]; + for (NSValue *valueOfRange in self.actions.allKeys) { + NSRange range = [valueOfRange rangeValue]; + CGRect rect = [self boundingRectForCharacterRange:range]; + if (CGRectContainsPoint(rect, location)){ + return [self.actions objectForKey:valueOfRange]; + } + } + return nil; +} + @end