1

此代码可以很好地使 url 出现在 UITextview

UITextView * descriptionTextView = [[UITextView alloc] initWithFrame:CGRectMake(10, 50, 300, 300)];
descriptionTextView.textColor = [UIColor whiteColor]; 
descriptionTextView.dataDetectorTypes = UIDataDetectorTypeLink;
descriptionTextView.font = [UIFont fontWithName:@"Helvetica" size:16];  
descriptionTextView.backgroundColor = [UIColor clearColor]; 
descriptionTextView.text = @"Click to go to the google website, http://www.google.com ";
descriptionTextView.autocorrectionType = UITextAutocorrectionTypeNo;    
descriptionTextView.keyboardType = UIKeyboardTypeDefault;  
descriptionTextView.returnKeyType = UIReturnKeyDone;  
descriptionTextView.editable = NO;
descriptionTextView.tag = 1;
[self.view addSubview:descriptionTextView];

问题是我写的整个网址都出现了,http://www.google.com

有没有办法只用一个词来包含链接?因此,用户只能看到以蓝色书写的“Goggle”,当他们点击该作品时,它会打开 safari。

非常感谢,-代码

4

2 回答 2

1

你可以使用UIWebview

采用

- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL;

并对整个消息进行编码,然后将链接包装到

[webView.loadHTMLString:@"<a href="http://www.google.com/">Google</a>" baseURL:nil];
于 2012-04-18T02:41:57.543 回答
0

我知道这是超级老线程。UITextView 支持富文本/HTML。您应该创建 NSAttributedString 并将其分配给属性文本属性以使用非纯文本内容进行操作。像这样:

        NSString *htmlString = @"<your HTML code>";
        textView.attributedText = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding]
                                                           options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                                                     NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
                                                documentAttributes:nil
                                                             error:nil];
于 2017-03-10T08:32:10.377 回答