题
我在UITextView
中有以下html,希望将其呈现为UITextView
是我的注意力
<a href="/arc/item/21">food item - more item stuff</a>;`
.
让我添加:它目前显示为蓝色和下划线而不是点击。我想使它粗体,不点击。我已经阅读了关于世代的文档,但是,没有使用过这个,它超出了我,我真的没有看到任何操作方法。我将如何渲染上面的链接粗体和黑色(不是蓝色)并保持不可点击的自然?
解决方案
更新(使用 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滚动,那么这可能是您的解决方案。
以前的(替代解决方案)
如果您使用的故事板/ XIB,请确保您取消选择检测 - >链接为您的UItextview。您可以通过将其容器字体设置为一些粗体字体来使链接粗体。如果要在一个字符串对象中支持不同的文本/字体样式,那么您应该真正查找 nsattributedstring 或 nsmutableattributtring 。
请参阅: https: //developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/nsattribedstring_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;
...
. 不隶属于 StackOverflow