Hyperlinks in a UITextView

后端 未结 5 1159
無奈伤痛
無奈伤痛 2020-12-01 16:18

I am trying to create a UITextView with a hyperlink so that when the user clicks on the link, they are taken to safari to open the we

5条回答
  •  佛祖请我去吃肉
    2020-12-01 16:53

    This code sample has two different links in the same label and URL color is set to avoid the default blue.

    UITextView * textTerm = [UITextView new];
    NSMutableAttributedString *attrRight = [[NSMutableAttributedString alloc] initWithString:@"Terms of Service"
                                                                           attributes:@{ NSLinkAttributeName: [NSURL URLWithString:@"http://www.google.com"] }];
    NSMutableAttributedString *attrLeft = [[NSMutableAttributedString alloc] initWithString:@"Privacy Policy"
                                                                           attributes:@{ NSLinkAttributeName: [NSURL URLWithString:@"http://www.google.com"] }];
    [attrRight appendAttributedString:attrLeft];
    textTerm.attributedText = attrRight;
    textTerm.editable = NO;
    textTerm.dataDetectorTypes = UIDataDetectorTypeAll;
    textTerm.linkTextAttributes = [UIColor whiteColor];
    textTerm.backgroundColor = [UIColor clearColor];
    

提交回复
热议问题