iOS clickable text inside UITextView

前端 未结 1 936
心在旅途
心在旅途 2020-12-29 14:36

Is there a way to make clickable parts of UITextView. Actually I want to make text something like

By clicking “Register” above, you are agreeing to t

相关标签:
1条回答
  • 2020-12-29 15:16

    I did it with the code above using this project

    - (void)_configureTermsLabel
    {
        self.termsOfUseLabel.hidden = YES;
        self.termsAndConditionsLabel = [[TTTAttributedLabel alloc] initWithFrame:self.termsOfUseLabel.frame];
        self.termsAndConditionsLabel.font = [UIFont systemFontOfSize:14];
        self.termsAndConditionsLabel.lineBreakMode = UILineBreakModeWordWrap;
        self.termsAndConditionsLabel.numberOfLines = 0;
    
        NSString *termsStr = NSLocalizedString(@"Terms of use", @"Terms of use");
        NSString *privacyStr = NSLocalizedString(@"Privacy Policy", @"Privacy Policy");
        NSString *andStr = NSLocalizedString(@"and", @"and");
        NSString *conductStr = NSLocalizedString(@"Code of conduct", @"Code of conduct");
        NSString *termsAndConditionsStr = [NSString stringWithFormat:@"%@ - %@ %@ %@", termsStr,
                                           privacyStr, andStr, conductStr];
        self.termsAndConditionsLabel.text = termsAndConditionsStr;
    
        NSString *languageCode = [[GLQAppDelegate sharedDelegate] languageIdentifier];
        NSURL *termsURL = [NSURL URLWithString:[NSString stringWithFormat:kGLQTermsOfUseURL, languageCode]];
        NSURL *privacyURL = [NSURL URLWithString:[NSString stringWithFormat:kGLQPrivacyPolicyURL, languageCode]];
        NSURL *conductURL = [NSURL URLWithString:[NSString stringWithFormat:kGLQCodeOfConductURL, languageCode]];
    
        NSRange termsRange = [self.termsAndConditionsLabel.text rangeOfString:termsStr];
        NSRange privacyRange = [self.termsAndConditionsLabel.text rangeOfString:privacyStr];
        NSRange conductRange = [self.termsAndConditionsLabel.text rangeOfString:conductStr];
    
        [self.termsAndConditionsLabel addLinkToURL:termsURL withRange:termsRange];
        [self.termsAndConditionsLabel addLinkToURL:privacyURL withRange:privacyRange];
        [self.termsAndConditionsLabel addLinkToURL:conductURL withRange:conductRange];
        self.termsAndConditionsLabel.delegate = self;
    
        self.termsAndConditionsLabel.userInteractionEnabled = YES;
        [self.scrollView addSubview:self.termsAndConditionsLabel];
    }
    
    0 讨论(0)
提交回复
热议问题