
設(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;
}