它似乎不可能用 UITextViews 做我想做的事情(至少不容易)。解决方案是改用 UIWebView 并加载带有文本和链接的本地 HTML 文件。你甚至可以放一些 CSS 来自定义颜色、大小...
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// webview
webview.delegate = (id<UIWebViewDelegate>)self;
NSString *path;
NSBundle *thisBundle = [NSBundle mainBundle];
path = [thisBundle pathForResource:@"fileName" ofType:@"html"];
NSError *error;
NSString *initialHTMLString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
[webview loadHTMLString:initialHTMLString baseURL:nil];
webview.opaque = NO;
webview.backgroundColor = [UIColor clearColor];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}
我希望这会帮助任何有同样问题的人!