更新(使用UITextView 的 linkTextAttributes 的解决方案)
self.testTextView.editable = NO;
self.testTextView.selectable = YES;
self.testTextView.userInteractionEnabled = NO; // workaround to disable link - CAUTION: it also disables scrolling of UITextView content
self.testTextView.dataDetectorTypes = UIDataDetectorTypeLink;
self.testTextView.linkTextAttributes = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:14.0f], // NOT WORKING !?
NSForegroundColorAttributeName : [UIColor redColor]};
...
self.testTextView.text = @"Lorem ipsum http://www.apple.com Lorem ipsum";
正如您在评论中看到的那样,我无法将新字体设置为 linkTextAttributes,尽管颜色属性按预期工作。
如果您可以使用颜色属性或其他一些文本属性来设置 URL 的样式,并且您不必担心禁用 UITextView 滚动,那么这可能是您的解决方案。
上一个(替代解决方案)
如果您使用的是 Storyboard/xib,请确保您已取消选择检测 -> UITextView 的链接。您可以通过将其容器字体设置为一些粗体来使您的链接加粗。如果您想在一个字符串对象中支持不同的文本/字体样式,那么您真的应该寻找NSAttributedString或NSMutableAttributedString。
请参阅:https ://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSAttributedString_Class/Reference/Reference.html 。
示例:
UIFont *linkFont = [UIFont fontWithName:@"SomeBoldTypeface" size:12];
NSString *link = @"food item - more item stuff";
NSMutableAttributedString *someString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"is my body for the note %@; let me ad", link]];
[someString addAttribute:NSFontAttributeName value:linkFont range:NSMakeRange(24, link.length)];
UITextView *textView = [[UITextView alloc] init];
textView.attributedText = someString;
...