0

I have the following HTML in a UITextView and would like to render it into a UITextView

is my body for the note

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

Let me add: it's currently showing as blue and underlined and not clickable. I would like to make it bolded and not clickable. I have read the docs regarding linkTextAttributes but, not having used this, it is a bit beyond me and I don't really see any easy way to manipulate this. How would I just render the above link bolded and black (not blue) and maintain the non-clickable nature?

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
timpone
  • 19,235
  • 36
  • 121
  • 211

1 Answers1

6

UPDATE (solution using UITextView's 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";

As you can see in comments, I wasn't able to set new font to linkTextAttributes, though the colour attribute was working as expected.

If you can get away with colour attribute or some other text attribute to style your URLs and you don't have to worry about disabled UITextView scrolling, then this may be your solution.


PREVIOUS (alternative solution)

If you're using Storyboard/xib then make sure you've deselected Detection -> Links for your UITextView. You can make your link bold by setting its container font to some bold typeface. If you want to support different text/font styles in one string object then you should really look for NSAttributedString or NSMutableAttributedString.

See: https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSAttributedString_Class/Reference/Reference.html.

Example:

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;
...
damirstuhec
  • 6,069
  • 1
  • 22
  • 39
  • thx Damir - I probably asked a poor question. My fault - what I really want to do is just squash that link into bold. Not deal with the link separately. Is there any way I can just tell it to handle links -> bold using linksWithAttributes. I am doing this all in code - that's one of the reasons, it's a bit of a problem. There will be hundreds of these. – timpone Feb 27 '14 at 10:15
  • `linksWithAttributes`: what do you mean by that? I can't recall this method.. correct/reference me if I'm wrong. – damirstuhec Feb 27 '14 at 10:20
  • thx Damir - need to get to sleep; it's 3am where I'm at. best – timpone Feb 27 '14 at 10:59
  • yes, it's in the UITextView docs https://developer.apple.com/library/ios/documentation/uikit/reference/uitextview_class/Reference/UITextView.html#//apple_ref/doc/uid/TP40006898-CH3-SW29 but there's really no information on it – timpone Feb 27 '14 at 21:54
  • hmmm... well, not getting rid of the underline. I tried this: textview.linkTextAttributes = @{NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone)}; based upon http://stackoverflow.com/questions/15328704/how-can-i-underline-text-in-ios-6 but no dice – timpone Feb 28 '14 at 02:29
  • @timpone what's your deployment target? I'm testing my solution on iOS 7 and I have no underline... – damirstuhec Feb 28 '14 at 08:38
  • Really, I have an underline. I'll accept answer but really am confused as to what's going on. – timpone Mar 03 '14 at 00:44