iOS-文本實(shí)現(xiàn)超鏈接

在做登錄或者注冊(cè)頁(yè)面時(shí),都會(huì)有有類似于協(xié)議閱讀的功能,左邊是一個(gè)選擇框是否閱讀,右邊是一行文本,且協(xié)議文本顏色不同。

之前在做的時(shí)候,因?yàn)橐恍形谋揪湍茱@示全,且僅僅只是一個(gè)協(xié)議,因此博主采取的方法如下:

1、在label覆蓋一個(gè)clearColor的UIButton,坐標(biāo)同label大小

2、給label上添加一個(gè)UITapGestureRecognizer手勢(shì)

但后期有些功能牽扯到金融以及安全問(wèn)題,需要多個(gè)協(xié)議以及多行顯示,這時(shí)要按之前方法去做的話,就需要截取當(dāng)前文本協(xié)議長(zhǎng)度以及位置,然后在相應(yīng)位置上添加buton和手勢(shì),做起來(lái)還是相對(duì)麻煩一下。因此博主想法用文本實(shí)現(xiàn)超鏈接那種方式百度一下,找到UITextView的一個(gè)代理方法,且這種方法能將前面的勾選框也顯示在富文本之前,但是自我感覺(jué)沒(méi)有直接創(chuàng)建UIImageView或者UIButton使用起來(lái)靈活,因?yàn)槲覀兛梢詫IButton的frame放大,手指觸摸面積增大

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

PS:UILabel控件是沒(méi)有這個(gè)代理方法的,因此只能創(chuàng)建添加UITextView。

1、首先我們要設(shè)置富文本以及添加鏈接

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.select = NO;
    
    [self setSubView];//設(shè)置子view
    [self setLinkText];//設(shè)置文本
}
 
//設(shè)置子view
- (void)setSubView{
    UIFont *linkFont = [UIFont systemFontOfSize:14.0];
    CGFloat linkW = ScreenWidth - 10*2;
    
    UITextView *linkTV = [[UITextView alloc]initWithFrame:CGRectMake(10, 100, linkW, 100)];
    self.linkTV = linkTV;
    linkTV.userInteractionEnabled = YES;
    linkTV.font = linkFont;
    linkTV.textColor = UIColorFromRGB(0x999999,1.0);
    [self.view addSubview:linkTV];
    linkTV.editable = NO;//必須禁止輸入,否則點(diǎn)擊將彈出輸入鍵盤
    linkTV.scrollEnabled = NO;
    linkTV.delegate = self;
    linkTV.textContainerInset = UIEdgeInsetsMake(0,0, 0, 0);//文本距離邊界值
}
 
//設(shè)置文本
- (void)setLinkText{
    NSString *linkStr = @"我已閱讀《登錄協(xié)議》和《注冊(cè)協(xié)議》,并且還有《支付寶支付協(xié)議》、《微信支付協(xié)議》,《中國(guó)工商銀行協(xié)議》、《中國(guó)銀行協(xié)議》、《中國(guó)建設(shè)銀行協(xié)議》、《中國(guó)農(nóng)業(yè)銀行協(xié)議》";
    UIFont *linkFont = [UIFont systemFontOfSize:14.0];
    CGFloat linkW = ScreenWidth - 10*2;
    CGSize linkSize = [self getAttributionHeightWithString:linkStr lineSpace:1.5 kern:1 font:linkFont width:linkW];
    self.linkTV.frame = CGRectMake(10, 100, linkW, linkSize.height);
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:linkStr];
    [attributedString addAttribute:NSLinkAttributeName value:@"login://" range:[[attributedString string] rangeOfString:@"《登錄協(xié)議》"]];
    [attributedString addAttribute:NSLinkAttributeName value:@"register://" range:[[attributedString string] rangeOfString:@"《注冊(cè)協(xié)議》"]];
    
    CGSize size = CGSizeMake(12, 12);
    UIImage *image = [UIImage imageNamed:self.select == YES ? @"selected" : @"unSelected"];
    UIGraphicsBeginImageContextWithOptions(size, false, 0);
    [image drawInRect:CGRectMake(0, 0.25, 12, 12)];
    UIImage *resizeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
    textAttachment.image = resizeImage;
    NSMutableAttributedString *imageString = (NSMutableAttributedString *)[NSMutableAttributedString attributedStringWithAttachment:textAttachment];
    [imageString addAttribute:NSLinkAttributeName value:@"checkbox://" range:NSMakeRange(0, imageString.length)];
    [attributedString insertAttributedString:imageString atIndex:0];
//    [attributedString addAttribute:NSFontAttributeName value:linkFont range:NSMakeRange(0, attributedString.length)];
    
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    //調(diào)整行間距
    paragraphStyle.lineSpacing = 1.5;
    NSDictionary *attriDict = @{NSParagraphStyleAttributeName:paragraphStyle,NSKernAttributeName:@(1),
                                NSFontAttributeName:linkFont};
    [attributedString addAttributes:attriDict range:NSMakeRange(0, attributedString.length)];
    
    self.linkTV.attributedText = attributedString;
    self.linkTV.linkTextAttributes = @{NSForegroundColorAttributeName: [UIColor blueColor], NSUnderlineColorAttributeName: [UIColor lightGrayColor], NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)};
}
 
/*
 *  設(shè)置行間距和字間距
 *
 *  @param string    字符串
 *  @param lineSpace 行間距
 *  @param kern      字間距
 *  @param font      字體大小
 *
 *  @return 富文本
 */
- (NSAttributedString *)getAttributedWithString:(NSString *)string WithLineSpace:(CGFloat)lineSpace kern:(CGFloat)kern font:(UIFont *)font{
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    //調(diào)整行間距
    paragraphStyle.lineSpacing = lineSpace;
    NSDictionary *attriDict = @{NSParagraphStyleAttributeName:paragraphStyle,NSKernAttributeName:@(kern),
                                NSFontAttributeName:font};
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:string attributes:attriDict];
    return attributedString;
}
 
/* 獲取富文本的高度
 *
 * @param string    文字
 * @param lineSpace 行間距
 * @param kern      字間距
 * @param font      字體大小
 * @param width     文本寬度
 *
 * @return size
 */
- (CGSize)getAttributionHeightWithString:(NSString *)string lineSpace:(CGFloat)lineSpace kern:(CGFloat)kern font:(UIFont *)font width:(CGFloat)width {
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    paragraphStyle.lineSpacing = lineSpace;
    NSDictionary *attriDict = @{
                                NSParagraphStyleAttributeName:paragraphStyle,
                                NSKernAttributeName:@(kern),
                                NSFontAttributeName:font};
    CGSize size = [string boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attriDict context:nil].size;
    return size;
}

2、響應(yīng)UITextView的代理方法,類似于UIButton的點(diǎn)擊事件

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
    if ([[URL scheme] isEqualToString:@"checkbox"]) {
        self.view.backgroundColor = [UIColor whiteColor];
        self.select = !self.select;
        [self setLinkText];//設(shè)置文本
        return NO;
    }else if ([[URL scheme] isEqualToString:@"login"]) {
        self.view.backgroundColor = [UIColor redColor];
        return NO;
    }else if ([[URL scheme] isEqualToString:@"register"]) {
        self.view.backgroundColor = [UIColor greenColor];
        return NO;
    }
    return YES;
}

image.png

摘自:https://blog.csdn.net/MinggeQingchun/article/details/77894277?locationNum=1&fps=1

最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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