8

I have a UITextView with text in it, the View is in a UITableViewCell. I noticed that the font was not quite the same on iOS7 as with iOS6, noting it was set to "system" I decided to specify the exact font/size.

It appeared nothing happened so I thought I would do a better test (big font not used anywhere), like this in my "CellForRowAt....";

cell.newsItemDescription.font = [UIFont fontWithName:@"didot" size:20];
cell.newsItemDescription.text = newsDescriptions[indexPath.row];

In iOS6 it comes out like this;

Screen Shot from iOS6

In iOS7 it comes out like this;

enter image description here

It happens in just a few places in the app but it is very annoying, can't figure out why? I am fast getting to the point where I may use the iOS7 Font/ Size throughout the app.

Some extra info;
The UITextView is resized per cell along with the cell (using springs/struts, i.e. no Auto Layout) and HeightForRow...

The font was setup in Storyboard originally (as system)

This is the same on devices and Simulator

Community
  • 1
  • 1
Recycled Steel
  • 2,272
  • 3
  • 30
  • 35

5 Answers5

32

I have a strange behavior in iOS 7. Font is smaller than I expect, if I was set it in to the xib.

If I set font after setting the text it's works for me. Otherwise font is smaller.

Try this:

  cell.newsItemDescription.text = newsDescriptions[indexPath.row];
  cell.newsItemDescription.font = [UIFont fontWithName:@"didot" size:20];
AlKozin
  • 904
  • 8
  • 25
  • Okay, so that worked!!! Got to change code around now, not too hard I suppose. Strange it does this, and that it takes no notice of Story Board. Well done. – Recycled Steel Oct 11 '13 at 09:46
  • 1
    You may also have to set the `editable` property in storyboard. http://stackoverflow.com/questions/19113673/uitextview-setting-font-not-working-with-ios-6-on-xcode-5 – qix Dec 06 '13 at 09:21
  • or the `selectable` property http://stackoverflow.com/questions/19049917/uitextview-font-is-being-reset-after-settext – Kaolin Fire Dec 11 '14 at 23:27
  • iOS 9.1 and this still bug(?) exists! Thanks! – Kedar Paranjape Dec 02 '15 at 07:04
6

I had the same issue today with UITextView, and while reading AlKozin's answer, I remembered something: somewhere, sometime I read that since iOS 7, the best practice to set font styles is to set them after the View has loaded. In other words, if I set everything in viewDidLoad: , nothing happens. You have to set up the font of your UITextView in viewWillAppear:, like this:

-(void)viewWillAppear:(BOOL)animated
{
     self.myTextView.font = [UIFont fontWithName:@"Baskerville-Italic" size:18.0];
}
suntime
  • 93
  • 1
  • 7
3

I ran into a weird bug (Xcode 7.2.1), where unchecking "Selectable" in IB was causing the UITextView to not adhere to the font settings specified through IB.

Reference Image

nenchev
  • 1,998
  • 28
  • 16
3

This is same issue I was facing.

Making UITextview "selectable" from storyboard will work.

Neeraj Joshi
  • 721
  • 9
  • 26
  • Can you give a code example of how to do this? The advice given appears to be different from the accepted answer and may be relevant to someone new to this area of development. – gabe3886 Apr 26 '16 at 10:49
  • @gabe3886 i've just changed it from storyboard nothing more. – Neeraj Joshi Apr 26 '16 at 12:16
0

Setting textview.font worked fine for me on iOS 7, but none of these answers, or other similar answers on other SO pages worked for me on iOS 8. The only way I was able to get it working was to use an NSAttributedString.

NSMutableParagraphStyle *pStyle = [[NSMutableParagraphStyle alloc] init];
[pStyle setAlignment:NSTextAlignmentCenter];
tv.attributedText = [[NSAttributedString alloc]
                     initWithString:text
                         attributes:@{ NSForegroundColorAttributeName: color,
                                       NSFontAttributeName: [UIFont systemFontOfSize:fontSize],
                                       NSParagraphStyleAttributeName: pStyle,
                                     } ];
prewett
  • 1,587
  • 14
  • 19