add actions into mflabel

This commit is contained in:
panxi 2019-04-01 12:01:44 -04:00
parent 8e0671f9bc
commit 560ea1030d
2 changed files with 34 additions and 0 deletions

View File

@ -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;

View File

@ -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<UITouch *> *)touches withEvent:(UIEvent *)event {
NSDictionary *actionMap = [self areTouchesInActionString:touches];
if (actionMap) {
[[MVMCoreActionHandler sharedActionHandler] handleActionWithDictionary:actionMap additionalData:nil delegate:nil];
}
}
- (NSDictionary *)areTouchesInActionString:(NSSet<UITouch *> *)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