TTTAttributedLabel控件的使用

(一)前言
前端

有時候項目會遇到,點擊一段話中的某一個或者是更多的關鍵字,須要執行某一個操做;這樣用button確定是行不通的了;TTTAttributedLabel第三方庫在不少app應用中都有使用,趁着如今沒事,簡單的瞭解一下這個控件。app


(二)相關的代碼;函數

(1)寫這段段簡單的代碼的時候也遇到了很多的坑,好比說設置前端的顏色(須要這樣寫(id)kCTForegroundColorAttributeName),使用NSForegroundColorAttributeName設置是不行的;若是不須要顯示下劃線的話,在linkAttributes字典中不須要加上下劃線的樣式(id)kCTUnderlineStyleAttributeName;url

TTTAttributedLabel *label = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(50, CGRectGetMaxY(btn.frame)+30, 200, 40)];
    label.delegate = self;
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor blackColor];
    label.backgroundColor = [UIColor lightGrayColor];
    label.enabledTextCheckingTypes = NSTextCheckingTypePhoneNumber;
    
    label.linkAttributes = @{(id)kCTForegroundColorAttributeName:[UIColor yellowColor]};
    
    [label setText:@"點擊 123456 自動撥打電話" afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
        
        NSRange boldRange = [[mutableAttributedString string] rangeOfString:@"123456" options:NSCaseInsensitiveSearch];
        [mutableAttributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20.0f] range:boldRange];
        
        return mutableAttributedString;
    }];
    
    label.activeLinkAttributes = @{(id)kCTForegroundColorAttributeName:[UIColor greenColor]};
    label.inactiveLinkAttributes = @{(id)kCTForegroundColorAttributeName:[UIColor blackColor]};
    
    NSRange testRange = [label.text rangeOfString:@"123456"];
    //[label addLinkToAddress:@{@"key":@"test"} withRange:testRange];
    
    [label addLinkToPhoneNumber:@"123456" withRange:testRange];

    [self.view addSubview:label];
    

//委託  
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithAddress:(NSDictionary *)addressComponents{
    
    NSLog(@"data -- %@",addressComponents);
}

- (void)attributedLabel:(TTTAttributedLabel *)label didLongPressLinkWithTransitInformation:(NSDictionary *)components atPoint:(CGPoint)point{
    
    NSLog(@"data -- %@",components);
}

- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url
{
    NSLog(@"url -- %@",url.absoluteString);
}

- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithPhoneNumber:(NSString *)phoneNumber{
    
    NSLog(@"number -- %@",phoneNumber);
}

注意:label.enabledTextCheckingTypes = NSTextCheckingTypePhoneNumber;這段代碼主要是自動檢測label.text字符串中相應類型的信息(好比NSTextCheckingTypePhoneNumber檢測的是電話號碼(格式必定要正確),字符串中出現的電話號碼都會被添加上點擊事件),點擊可觸發對應的委託事件;就不須要顯示的調用方法:[label addLinkToPhoneNumber:@"123456" withRange:testRange];若是未設置次屬性,則須要自行添加對應的點擊事件。code


(三)TTTAttributedLabel一些代碼解析;component

(1)linkAttributes屬性和其餘的activeLinkAttributes和inactiveLinkAttributes屬性都是可配置的,linkAttributes未設置會默認的顯示下劃線以及blueColor的前景色;如設置了則會顯示設置的樣式;orm

(2)控件中TTTAttributedLabelDelegate的委託協議,好比[label addLinkToPhoneNumber:@"123456" withRange:testRange];點擊label上的「123456」;會執行- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithPhoneNumber:(NSString *)phoneNumber對應的這個點擊委託事件;爲何會執行這個委託事件呢,緣由是在- (void)touchesEnded:(NSSet *)touches  withEvent:(UIEvent *)event這個點擊事件中;對NSTextCheckingResult的類型進行了判斷,執行對應的委託函數;事件

(3)下面的代碼是設置label樣式的函數以及執行委託判斷的事件;字符串

- (TTTAttributedLabelLink *)addLinkWithTextCheckingResult:(NSTextCheckingResult *)result
                                               attributes:(NSDictionary *)attributes
{
    return [self addLinksWithTextCheckingResults:@[result] attributes:attributes].firstObject;
}

- (NSArray *)addLinksWithTextCheckingResults:(NSArray *)results
                                  attributes:(NSDictionary *)attributes
{
    NSMutableArray *links = [NSMutableArray array];
    
    for (NSTextCheckingResult *result in results) {
        NSDictionary *activeAttributes = attributes ? self.activeLinkAttributes : nil;
        NSDictionary *inactiveAttributes = attributes ? self.inactiveLinkAttributes : nil;
        
        TTTAttributedLabelLink *link = [[TTTAttributedLabelLink alloc] initWithAttributes:attributes
                                                                         activeAttributes:activeAttributes
                                                                       inactiveAttributes:inactiveAttributes
                                                                       textCheckingResult:result];
        
        [links addObject:link];
    }
    
    [self addLinks:links];
    
    return links;
}

- (TTTAttributedLabelLink *)addLinkWithTextCheckingResult:(NSTextCheckingResult *)result {
    return [self addLinkWithTextCheckingResult:result attributes:self.linkAttributes];
}

//判斷執行那個委託事件;
- (void)touchesEnded:(NSSet *)touches
           withEvent:(UIEvent *)event
{
    if (self.activeLink) {
        if (self.activeLink.linkTapBlock) {
            self.activeLink.linkTapBlock(self, self.activeLink);
            self.activeLink = nil;
            return;
        }
        
        NSTextCheckingResult *result = self.activeLink.result;
        self.activeLink = nil;

        switch (result.resultType) {
            case NSTextCheckingTypeLink:
                if ([self.delegate respondsToSelector:@selector(attributedLabel:didSelectLinkWithURL:)]) {
                    [self.delegate attributedLabel:self didSelectLinkWithURL:result.URL];
                    return;
                }
                break;
            case NSTextCheckingTypeAddress:
                if ([self.delegate respondsToSelector:@selector(attributedLabel:didSelectLinkWithAddress:)]) {
                    [self.delegate attributedLabel:self didSelectLinkWithAddress:result.addressComponents];
                    return;
                }
                break;
            case NSTextCheckingTypePhoneNumber:
                if ([self.delegate respondsToSelector:@selector(attributedLabel:didSelectLinkWithPhoneNumber:)]) {
                    [self.delegate attributedLabel:self didSelectLinkWithPhoneNumber:result.phoneNumber];
                    return;
                }
                break;
            case NSTextCheckingTypeDate:
                if (result.timeZone && [self.delegate respondsToSelector:@selector(attributedLabel:didSelectLinkWithDate:timeZone:duration:)]) {
                    [self.delegate attributedLabel:self didSelectLinkWithDate:result.date timeZone:result.timeZone duration:result.duration];
                    return;
                } else if ([self.delegate respondsToSelector:@selector(attributedLabel:didSelectLinkWithDate:)]) {
                    [self.delegate attributedLabel:self didSelectLinkWithDate:result.date];
                    return;
                }
                break;
            case NSTextCheckingTypeTransitInformation:
                if ([self.delegate respondsToSelector:@selector(attributedLabel:didSelectLinkWithTransitInformation:)]) {
                    [self.delegate attributedLabel:self didSelectLinkWithTransitInformation:result.components];
                    return;
                }
            default:
                break;
        }

        // Fallback to `attributedLabel:didSelectLinkWithTextCheckingResult:` if no other delegate method matched.
        if ([self.delegate respondsToSelector:@selector(attributedLabel:didSelectLinkWithTextCheckingResult:)]) {
            [self.delegate attributedLabel:self didSelectLinkWithTextCheckingResult:result];
        }
    } else {
        [super touchesEnded:touches withEvent:event];
    }
}


(四)總結;
string

以上只是本人對這個控件的部分代碼的一些理解,也許會有理解錯誤的地方,但願各位技術同仁可以留言、評論指出在下錯誤的地方,有更好的理解也能夠交流交流。

相關文章
相關標籤/搜索