iOS中使用UITextView設(shè)置不同文本部分點(diǎn)擊事件小結(jié)

最近在項(xiàng)目開(kāi)發(fā)中遇到了
image

設(shè)置多行文本,點(diǎn)擊藍(lán)色郵箱部分跳轉(zhuǎn)到發(fā)郵件頁(yè)面功能。當(dāng)然比較簡(jiǎn)單的方式是多標(biāo)簽單獨(dú)設(shè)置,那樣稍顯麻煩。我們能不能用一個(gè)控件,給某一部分添加點(diǎn)擊事件,結(jié)果是可以的,UITextView完美實(shí)現(xiàn)這個(gè)功能,代碼如下。

1、添加UITextView的代理UITextViewDelegate

2、@property (nonatomic, strong) UITextView *textRemind;//設(shè)置UITextView屬性變量

  • (UITextView *)textRemind{

    if (_textRemind == nil) {

      _textRemind = [[UITextView alloc]init];
    
      _textRemind.backgroundColor = [UIColor greenColor];
    
      _textRemind.textColor=K_CC_COLOR_STRING(@"#999999");
    
      //_textRemind.textAlignment = NSTextAlignmentRight;//此處沒(méi)有效果,需要在屬性里面單獨(dú)設(shè)置
    
      _textRemind.font = K_CC_FONT_SEMIBOLD(14);
    

    }

    return _textRemind;

}

//頂部菜單標(biāo)題

NSString *firstStr=@"此功能暫時(shí)無(wú)法使用,請(qǐng)聯(lián)系\n";

NSString *secondStr=@"guanshuai@cloudcc.com\n";

NSString *thirdStr=@"或\n";

NSString *fourthStr=@"rjr@cloudcc.com\n";

NSString *fifthStr=@"開(kāi)通";

NSString *remindStr=[NSString stringWithFormat:@"%@%@%@%@%@",firstStr,secondStr,thirdStr,fourthStr,fifthStr];

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:remindStr];

//給需要點(diǎn)擊的部分添加關(guān)鍵字

[attributedString addAttribute:NSLinkAttributeName

                        value:@"firstmanager"

                        range:[[attributedString string] rangeOfString:secondStr]];

[attributedString addAttribute:NSLinkAttributeName

                        value:@"secondmanager"

                        range:[[attributedString string] rangeOfString:fourthStr]];

//設(shè)置水平中間對(duì)齊

NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init];

[paragrahStyle setAlignment:NSTextAlignmentCenter];

[attributedString addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:NSMakeRange(0, remindStr.length)];

self.textRemind.attributedText = attributedString;

//設(shè)置點(diǎn)擊部分的文字顏色

self.textRemind.linkTextAttributes = @{NSForegroundColorAttributeName: K_CC_COLOR_STRING(@"#2D6CFC") };

self.textRemind.editable = NO; //必須禁止輸入,否則點(diǎn)擊將彈出輸入鍵盤(pán)

self.textRemind.scrollEnabled = NO;

self.textRemind.selectable = NO;

self.textRemind.delegate = self;

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addGestureRecognizer:)];

[self.textRemind addGestureRecognizer:tapRecognizer];

[self.view addSubview:self.textRemind];

[self.textRemind mas_makeConstraints:^(MASConstraintMaker *make) {

    make.center.equalTo(self.view);

    make.height.mas_equalTo(100);

    make.width.mas_equalTo(K_CC_SCREEN_WIDTH-32);

}];

3、添加手勢(shì)處理

-(void)addGestureRecognizer:(UIGestureRecognizer*)gestureRecognizer{

if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]])

{

    CGPoint tapLocation = [gestureRecognizer locationInView:self.textRemind];

    UITextPosition *textPosition = [self.textRemind closestPositionToPoint:tapLocation];

    NSDictionary *attributes = [self.textRemind textStylingAtPosition:textPosition inDirection:UITextStorageDirectionBackward];

    NSURL *url = attributes[NSLinkAttributeName];

    if(url) {

        NSRange range = [self.textRemind.text rangeOfString:@"guanshuai@cloudcc.com\n"];

        if (([url isKindOfClass:[NSString class]] && [(NSString *)url isEqualToString:@"firstmanager"])) {

            range = [self.textRemind.text rangeOfString:@"guanshuai@cloudcc.com\n"];

        } else if(([url isKindOfClass:[NSString class]] && [(NSString *)url isEqualToString:@"secondmanager"])){

            range = [self.textRemind.text rangeOfString:@"rjr@cloudcc.com\n"];

        }

        [self  textView:self.textRemind shouldInteractWithURL:url inRange:range];

      }

    }

}

  • (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{

    if ([(NSString *)URL isEqualToString:@"firstmanager"]) {

      return NO;
    

    } else if ([(NSString *)URL isEqualToString:@"secondmanager"]) {

      return NO;
    

    }

    return YES;

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容