iOS clickable text inside UITextView

本小妞迷上赌 提交于 2019-11-29 03:28:29

问题


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 the Terms of Services and Privacy Statement

where Terms of Services should be one link and Privacy Statement another. And by clicking on those I should do something.


回答1:


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];
}


来源:https://stackoverflow.com/questions/14338229/ios-clickable-text-inside-uitextview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!