






#import "FontReplaceLabel.h"
#import "YahooKeyTip.h"
#import "UIImage+DidImage.h"

@implementation FontReplaceLabel

+ (FontReplaceLabel *)millNotFixPenLabel {
    return [self millNotFixPenLabel:YES];
}

+ (FontReplaceLabel *)millNotFixPenLabel:(BOOL)isCheckBox {
    
    FontReplaceLabel *label = [[FontReplaceLabel alloc] init];
    label.numberOfLines = 0;
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor lightGrayColor];
    label.font = [UIFont systemFontOfSize:12];
    label.userInteractionEnabled = YES; 

    NSAttributedString *exponentRebuildCommandConditionHalf = nil;
    if (isCheckBox) {
        
        NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
        UIImage *templateImage = [[UIImage badmintonScanLogWorkflowShadowName:YahooKeyTip.tatarMaximum.numberFileComposePlaybackIllDisk] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        attachment.image = templateImage; 
        
        attachment.bounds = CGRectMake(0, -5, 20, 20);
        exponentRebuildCommandConditionHalf = [NSAttributedString attributedStringWithAttachment:attachment];
    }

    
    NSString *text = YahooKeyTip.somaliLoopMove.fitIcyFolder;
    NSMutableAttributedString *shotFillMemory = [[NSMutableAttributedString alloc] initWithString:text];
    
    
    NSRange buttonIdentify = [text rangeOfString:YahooKeyTip.somaliLoopMove.takeNextRepair];
    if (buttonIdentify.location != NSNotFound) {
        [shotFillMemory addAttribute:NSForegroundColorAttributeName value:[YahooKeyTip expireCanColor] range:buttonIdentify];
        [shotFillMemory addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:buttonIdentify];
    }

    
    NSMutableAttributedString *retainedBusyWrapperKindMap = [[NSMutableAttributedString alloc] init];
    if (exponentRebuildCommandConditionHalf) {
        [retainedBusyWrapperKindMap appendAttributedString:exponentRebuildCommandConditionHalf];
    }
    [retainedBusyWrapperKindMap appendAttributedString:shotFillMemory];
    
    label.attributedText = retainedBusyWrapperKindMap;
    
    
    UITapGestureRecognizer *quickReply = [[UITapGestureRecognizer alloc] initWithTarget:label action:@selector(frameMismatchAboutDateNanograms:)];
    [label addGestureRecognizer:quickReply];
    
    return label;
}

- (void)setClickThousands:(BOOL)clickThousands {
    _clickThousands = !clickThousands;
    [self ownYesterdayTempBuddhistCenteringAnyLabel:self];
}

- (void)frameMismatchAboutDateNanograms:(UITapGestureRecognizer *)quickReply {
    FontReplaceLabel *label = (FontReplaceLabel *)quickReply.view;
    if (!label.attributedText) return;
    
    
    NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:label.attributedText];
    NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
    NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:label.bounds.size];
    
    textContainer.lineFragmentPadding = 0;
    textContainer.maximumNumberOfLines = label.numberOfLines;
    textContainer.lineBreakMode = label.lineBreakMode;
    
    [textStorage addLayoutManager:layoutManager];
    [layoutManager addTextContainer:textContainer];
    
    
    [layoutManager ensureLayoutForTextContainer:textContainer];
    
    
    CGPoint boxOccurred = [quickReply locationInView:label];
    CGRect usedRect = [layoutManager usedRectForTextContainer:textContainer];
    CGPoint desktopOddCompareVariationManager = CGPointMake(
        (label.bounds.size.width - usedRect.size.width) / 2,   
        (label.bounds.size.height - usedRect.size.height) / 2  
    );
    
    
    CGPoint detailedSpecificMenHasFixPhotos = CGPointMake(
        boxOccurred.x - desktopOddCompareVariationManager.x,
        boxOccurred.y - desktopOddCompareVariationManager.y
    );
    
    
    __block BOOL replaceIndent = NO;
    [label.attributedText enumerateAttribute:NSAttachmentAttributeName
                                    inRange:NSMakeRange(0, label.attributedText.length)
                                    options:0
                                 usingBlock:^(id value, NSRange range, BOOL *stop) {
        if ([value isKindOfClass:[NSTextAttachment class]]) {
            
            NSRange sawArchive;
            [layoutManager glyphRangeForCharacterRange:range actualCharacterRange:&sawArchive];
            
            
            CGRect cocoaRect = [layoutManager boundingRectForGlyphRange:sawArchive
                                                      inTextContainer:textContainer];
            
            
            CGRect trapZoneBagRect = CGRectOffset(cocoaRect, desktopOddCompareVariationManager.x, desktopOddCompareVariationManager.y);
            
            
            if (CGRectContainsPoint(trapZoneBagRect, boxOccurred)) {
                replaceIndent = YES;
                *stop = YES;
            }
        }
    }];
    
    if (replaceIndent) {
        
        
        [self ownYesterdayTempBuddhistCenteringAnyLabel:label];
        return;
    }
    
    
    NSUInteger characterIndex = [layoutManager characterIndexForPoint:detailedSpecificMenHasFixPhotos
                                                    inTextContainer:textContainer
                           fractionOfDistanceBetweenInsertionPoints:NULL];
    
    NSString *faceText = label.attributedText.string;
    NSRange buttonIdentify = [faceText rangeOfString:YahooKeyTip.somaliLoopMove.takeNextRepair];
    
    if (characterIndex != NSNotFound && NSLocationInRange(characterIndex, buttonIdentify)) {
        
        
        if (self.escapedSleepSymbolsSpecialNeedBridge) {
            self.escapedSleepSymbolsSpecialNeedBridge();
        }
    }
}


- (void)ownYesterdayTempBuddhistCenteringAnyLabel:(FontReplaceLabel *)label {
    NSMutableAttributedString *attributedText = [label.attributedText mutableCopy];
    __block BOOL busFootAir = NO;
    
    [attributedText enumerateAttribute:NSAttachmentAttributeName
                             inRange:NSMakeRange(0, attributedText.length)
                             options:0
                          usingBlock:^(NSTextAttachment *oldAttachment, NSRange range, BOOL *stop) {
        if (![oldAttachment isKindOfClass:[NSTextAttachment class]]) return;
        
        
        BOOL arrayGasp = !_clickThousands;
        
        
        NSTextAttachment *dueAboveThree = [[NSTextAttachment alloc] init];
        
        
        UIColor *biotinColor = arrayGasp ? [YahooKeyTip expireCanColor]: UIColor.lightGrayColor;
        UIImage *agentSunImage = [UIImage badmintonScanLogWorkflowShadowName:arrayGasp ? YahooKeyTip.tatarMaximum.nameRepairAssertPairCelticApply :YahooKeyTip.tatarMaximum.numberFileComposePlaybackIllDisk];
        
        
        dueAboveThree.image = [[agentSunImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]
                                imageWithTintColor:biotinColor];
        dueAboveThree.bounds = oldAttachment.bounds;
        
        
        [attributedText removeAttribute:NSAttachmentAttributeName range:range];
        [attributedText addAttribute:NSAttachmentAttributeName value:dueAboveThree range:range];
        
        _clickThousands = arrayGasp;
        busFootAir = YES;
        *stop = YES;
    }];
    
    if (busFootAir) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [UIView transitionWithView:label
                              duration:0.3
                               options:UIViewAnimationOptionTransitionCrossDissolve
                            animations:^{
                                label.attributedText = attributedText;
                            } completion:nil];
            [label setNeedsDisplay];
        });
    }
}

@end
