Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView font is nil

I created a UITextView with a font size of 14 in a storyboard and hooked it up to the detailDescriptionLabel property of my ViewController. This code is in viewDidLoad:

self.detailDescriptionLabel.font=[UIFont systemFontOfSize:10]; NSLog(@"text is %@; font is %@", self.detailDescriptionLabel.text, self.detailDescriptionLabel.font); 

The console output follows:

text is Lorem Ipsum; font is (null) 

Why is the font set to nil? The setFont: is working; the font does shrink. I want to get the font so after a gesture I can call lineHeight on the font. This way, I can find out which line has been tapped with the following code:

int line = [tap locationInView:tap.view].y / self.detailDescriptionLabel.font.lineHeight; 

Here, too, the font is nil. line is set to -2147483648, which is obviously not what I want.

like image 864
L D Avatar asked Oct 19 '13 21:10

L D


1 Answers

Try checking the "selectable" checkbox for this UITextView in Interface Builder. It's in the Attributes Inspector. Per @VahramDodoryan's comment below, you can then set selectable to false if you don't want to support selection.

I can't explain why this works, but it's probably a UIKit bug. I had an IBOutlet to a UITextView whose font property was nil, and it would not respond to any font or text-color changes in code until after its text property had been set. I arrived at this solution through trial-and-error.


If you're still encountering this issue on recent releases of iOS, consider opening a radar:

  • https://openradar.appspot.com/15854592
  • https://openradar.appspot.com/15196932
like image 60
Ty Cobb Avatar answered Sep 30 '22 17:09

Ty Cobb