How to get current attribute string in non-editable UITextView?

I set the appearance of the UITextView text in the storyboard through the attribute properties of the string. However, when I try to access it at runtime to copy an attribute object, it returns null.

NSAttributedString *atrString = self.contentTextView.attributedText; NSLog(@"atrString = %@", atrString); 

Outputs

 2013-09-20 14:44:19.572 PageTest[69125:70b] atrString = (null) 

I haven’t worked with string attributes before, so I'm sure I’m doing something wrong, but clearing the documentation left me empty anyway. Any help would be appreciated. Thanks!

+7
ios cocoa-touch uitextview nsattributedstring
source share
2 answers

You had the same problem: you could not access the Text attribute in the TextView unless it is marked as selected in Interface Builder (Xcode 6.1). No problem though, if the same is done in code. In my case, I subclass UITextview:

 -(id)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if (self) { self.selectable = NO; } return self; } 

So, I assume that with IB is not something not quite perfect. Yet again)

+3
source share

It should work. Maybe you forgot to connect the IBOutlet contentTextView to the storyboard?

Try printing the contentTextView object:

 NSLog(@"contentTextView = %@", self.contentTextView); 
0
source share

All Articles