Question

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?

Was it helpful?

Solution

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;
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top