0

我在 a 中有以下 HTML,UITextView并希望将其呈现为UITextView

是我的身体作为笔记

<a href="/arc/item/21">food item - more item stuff</a>;`

让我补充一下:它当前显示为蓝色并带有下划线且不可点击。我想让它加粗并且不可点击。我已经阅读了有关的文档,linkTextAttributes但是没有使用它,它有点超出我的能力,我真的没有看到任何简单的方法来操纵它。我如何将上面的链接渲染为粗体和黑色(不是蓝色)并保持不可点击的性质?

4

1 回答 1

5

更新(使用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 的链接。您可以通过将其容器字体设置为一些粗体来使您的链接加粗。如果您想在一个字符串对象中支持不同的文本/字体样式,那么您真的应该寻找NSAttributedStringNSMutableAttributedString

请参阅: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;
...
于 2014-02-27T09:42:09.083 回答