34

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.

cojoj
  • 6,405
  • 4
  • 30
  • 52
L D
  • 339
  • 3
  • 3

3 Answers3

115

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:

Ty Cobb
  • 1,250
  • 1
  • 8
  • 7
  • 2
    I don't know why this is required (on iOS7), but it worked for me as well. Once I set the "selectable" box in IB I could query the textField for the font. Text being set to the textField in iOS7 was not adopting the font so I query the field, set the text, and reset the font, which makes it work. – chadbag Nov 06 '13 at 20:18
  • Thank you for narrowing this down! I was about to ditch storyboards because I need the font to calculate the size of the view. – JohnQ Nov 26 '13 at 13:27
  • It sounds crazy, but checking Selectable also fixed my issue. – phatmann Jan 20 '14 at 19:38
  • 3
    this definitely should be marked as answer. thanks a lot Ty Cobb! – Denis Feb 08 '14 at 18:56
  • 17
    You can set textView.selectable = NO property in code to make it non-selectable without this weird issue. – vdd Apr 01 '14 at 00:36
  • 3
    Is there any perceivable reason why Apple have set it up like this? Or should this be filed as a bug with Apple? – jowie May 15 '14 at 10:32
  • FWIW, Same situation in iOS9. Checking 'selectable' on the UITextView let getFont return the font rather than nil. – John Fowler Dec 11 '15 at 01:04
  • Thanks a lot. Any idea why textfield is behaving like this? – Dipu Rajak Feb 26 '16 at 19:25
  • Still same situation in iOS9. Checking 'selectable' on the UITextView let getFont return the font ... – Nazir May 31 '16 at 14:05
  • Same for iOS 10 also. Its awkward since a selectable UITextField can often have a weird impact on rendering performance, so I manually have to re-apply a font after making the view selectable. – Graham Nov 08 '16 at 11:16
  • 1
    I didnt experience this bug in iOS 10. only in iOS 9 for the textview. – hasan Jan 31 '17 at 13:33
  • 1
    Disputing hasan83's assertion that the bug is not present in iOS 10. I am seeing it as well. – software evolved Apr 03 '17 at 22:02
  • I had this issue in iOS 11 and this fixed it for me. – tagy22 Apr 12 '18 at 13:32
0

I tried changing every property and constraint , I had on my text view.

Including selectable as suggested above

In frustration in the end I deleted and re-added it and that was the unsatisfying fix.

Ryan Heitner
  • 13,119
  • 6
  • 77
  • 119
-1

You should try logging self.detailDescriptionLabel. The value might be null.

CodeHelp
  • 1,328
  • 5
  • 21
  • 37