iOS UITextview的attributestr超链颜色修改

超链和下划线 12 Pro - 2021-01-11 at 09.23.56.png
// 基本设置
    NSString *policyPromptStr = @"By signing in, you agree to our Terms of use and Privacy policy.";
    NSDictionary *attributes1 = [[CDCommon sharedCommon].appStyle isEqualToString:@"night"]? @{NSFontAttributeName : FONT_SIZE_REGULAR_SCALE(25, kFontScaleInEn),NSForegroundColorAttributeName:[CDCommon colorForKey:@"signInTerms1TextColor"]} : @{NSFontAttributeName : FONT_SIZE_REGULAR_SCALE(25, kFontScaleInEn)};
    //调整行间距
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.maximumLineHeight = FontHeightTitle(38);
    paragraphStyle.minimumLineHeight = FontHeightTitle(38);
    
    CGFloat termsWidth = LAYOUT_SCREENSIZE_P.width - LAYOUT_SIZESCALE_PORTRAIT(40, 40).width *2;
    CGFloat termsHeight = [BFLCommon contentLabelHeightWithText:policyPromptStr labelWidth:termsWidth font:FONT_SIZE_REGULAR_SCALE(25, kFontScaleInEn) paragraphStyle:paragraphStyle];
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:policyPromptStr attributes:attributes1];
    [attStr addAttributes:@{NSFontAttributeName : FONT_SIZE_REGULAR_SCALE(25, kFontScaleInEn),NSUnderlineStyleAttributeName : [NSNumber numberWithInteger:NSUnderlineStyleSingle],NSUnderlineColorAttributeName : [CDCommon colorForKey:@"menuViewBackgColor"],NSLinkAttributeName:kAppTermsofUse} range:NSMakeRange(32, 12)];
    [attStr addAttributes:@{NSFontAttributeName : FONT_SIZE_REGULAR_SCALE(25, kFontScaleInEn),NSUnderlineStyleAttributeName : [NSNumber numberWithInteger:NSUnderlineStyleSingle],NSUnderlineColorAttributeName : [UIColor greenColor],NSLinkAttributeName:kAppPrivacyPolicy} range:NSMakeRange(49, 14)]; //超链和下划线
    
    UITextView *policyTextView = [[UITextView alloc] initWithFrame:CGRectMake(LAYOUT_SIZESCALE_PORTRAIT(40, 40).width, LAYOUT_SIZESCALE_PORTRAIT(340,340).height, termsWidth, termsHeight)];
    policyTextView.delegate = self;
    policyTextView.editable = NO;
    policyTextView.scrollEnabled = NO;
    policyTextView.attributedText = attStr;
    policyTextView.backgroundColor = [UIColor clearColor];
    policyTextView.textContainerInset = isiPad ?UIEdgeInsetsMake(LAYOUT_SIZESCALE_PORTRAIT(10,10).height, 0,0,0) : UIEdgeInsetsMake(LAYOUT_SIZESCALE_PORTRAIT(5,5).height, 0,0,0);
    [self addSubview:policyTextView];
    policyTextView.linkTextAttributes = @{NSForegroundColorAttributeName : [UIColor redColor]}; //超链颜色修改

你可能感兴趣的:(iOS UITextview的attributestr超链颜色修改)