Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

moonman239

Cancelled
Original poster
Mar 27, 2009
1,541
32
Hi guys,

First of all, I wish to thank you for the tremendous help you have offered me, a novice iOS developer.

OK, I now have a new problem to solve. My app has a bunch of text views and the font sizes are too small. But there are so many text views that it'd take me forever to set the font size on all of them. So I want to set the size programmatically.

I have started with this code, which is located in FirstViewController.m, where my text views are:

Code:
-(void) viewDidLoad
UIFont *theTextviewFont = [fontWithName: @"Arial" size:20];
@end

Now I just need to apply this font to all the text views. Once again, I will appreciate any help I can get!
 
If you are truly lazy you should be able to write a simple method that recursively traverses a view's subviews looking for textViews or if you like any view that responds to setFont: and then sets the font on it.

You could even have a UIViewController that does this in its viewDidLoad method and then derive all your view controllers from that base class.
 
What? Why not? :confused:
I tried what I thought would work, and it didn't. I can't find any Web pages with helpful information on this subject.

If you are truly lazy you should be able to write a simple method that recursively traverses a view's subviews looking for textViews or if you like any view that responds to setFont: and then sets the font on it.

You could even have a UIViewController that does this in its viewDidLoad method and then derive all your view controllers from that base class.

I'll try what you suggested. I do have a question. All the view controllers of concern here are linked to a table view controller, which in turn is linked to a navigation view controller. I can see that the posterity (for lack of a better word) infer the simulated metrics from their parents. Because of this, the view controllers have a functional navigation bar with a functional back button. Does this mean that all the view controllers are automatically derived from the base class of the view controller at the top of the tree? I assume the answer is yes.

EDIT: I'm going to try every piece of code I have and see what works. When I find that piece of code, I'll be sure to post it here. I now see that I might have been wrong to assume that using the UIAppearance proxy would not work.
 
Last edited:
OK, I think I'm on to something here. Here's the code I tried. It doesn't work.
EDIT: I should add that I don't see any errors.

Code:
- (void)viewDidLoad
{
    [super viewDidLoad];
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
    UIFont *theTextViewFont = [UIFont fontWithName: @"Arial" size: 50.0f];
    NSAttributedString *textViewText = [[UITextView appearance] attributedText];
    NSMutableAttributedString *modifiedTextViewText = [[NSMutableAttributedString alloc] initWithAttributedString:textViewText];
    [modifiedTextViewText addAttribute:NSFontAttributeName value:theTextViewFont range:NSMakeRange(0, [textViewText length])];
    [[UITextView appearance] setAttributedText:modifiedTextViewText];
}
 
Sorry for leading you astray, moonman. After some digging it looks like UITextView doesn't have any UIAppearance customization (i.e. no relevant accessor methods marked with UI_APPEARANCE_SELECTOR) beyond what it inherits from UIScrollView. My apologies.
 
OK, will someone please help me? In FirstViewController.m, this is my new code:

Code:
- (void)viewDidLoad
{
    [super viewDidLoad];
    UIFont *theTextViewFont = [UIFont fontWithName: @"Arial" size: 50.0];
    [self setEveryTextViewFont:self theTVFont:theTextViewFont];
}
-(void) setEveryTextViewFont: (UIViewController *)viewController theTVFont:(UIFont *)theFont {
    for (UITextView *textView in [self.view subviews])
    {
        textView.font = theFont;
    }
   for (UIViewController *childVC in viewController.childViewControllers)
   {
       
       [self setEveryTextViewFont:self theTVFont:theFont];
   }
}

It doesn't work!
 
Your recursion looks messed up. You're looping across childVC instances but then calling setEveryTextViewFont: with self and never using childVC.

P.S. I would probably also add a check to ensure that each subview is a UITextView, just to be safe.
 
If I understand what you said I think you believe that having several view controllers in a navigation controller stack relates those view controllers by inheritance. That's not the case.

I would start out with a method that recursively iterates over a view controller's view looking for text views, and sets their font. Your code kind of does that but it doesn't look at subviews below the view controller's view. It needs to do that.

Don't try to access another view controller's views in this code.
 
Your recursion looks messed up. You're looping across childVC instances but then calling setEveryTextViewFont: with self and never using childVC.
Thank you for pointing that out. It's no wonder the code didn't work. I just changed that line to
Code:
[self setEveryTextViewFont:childVC theTVFont:theFont];
.

If I understand what you said I think you believe that having several view controllers in a navigation controller stack relates those view controllers by inheritance. That's not the case.

I would start out with a method that recursively iterates over a view controller's view looking for text views, and sets their font. Your code kind of does that but it doesn't look at subviews below the view controller's view. It needs to do that.

I now understand that the view controllers are not related simply because they're linked by push segues to the first view controller. At first, I didn't understand your second paragraph. I see that what you're saying is each view controller has a view, and those views have subviews. Edit: After taking another look at the code, it now seems like you must be mistaken. The first for loop in setEveryTextViewFont searches for UITextViews in the [self.view subviews] array.

Like I said, I just fixed the issue dejo mentioned, and I'm going to run the code again. I bet it will work, although I've noticed that sometimes, code that I think should work doesn't.
 
OK, this time I think I might have a solution. I dropped the following code in my AppDelegate.m file.

Code:
-(void)setFontToEveryTextViewInView:(UIFont *)textViewFont parentView: (UIView *)parentView
{
    NSLog(@"setFontToEveryTextView");
    NSUInteger *X = 1;
    NSArray *subViews = [parentView subviews];
    NSUInteger *theLength = [subViews count];
    while (X <= theLength)
    {
        if ([[subViews objectAtIndex:X] isKindOfClass:[UITextView class]])
        {
            UITextView *textView = [subViews objectAtIndex:X];
            NSLog(@"Found a text view.");
            textView.font = textViewFont;
        }
        else
        {
            [self setFontToEveryTextViewInView:textViewFont parentView:[subViews objectAtIndex:X]];
        }
    X = X + 1;
    }
}

-(void)setFontToEveryTextViewInViewController: (UIFont *)textViewFont parentVC: (UIViewController *)parentVC
{
    NSLog(@"setFontToEveryTextViewInViewController");
    [self setFontToEveryTextViewInView:textViewFont parentView:parentVC.view];
    NSArray *childVCs = parentVC.childViewControllers;
    NSUInteger *X = 1;
    NSUInteger *theLength = [childVCs count];
    while (X <= theLength)
    {
        [self setFontToEveryTextViewInViewController:textViewFont parentView:[childVCs objectAtIndex:X]];
    }
}

All I need to do is call setFontToEveryTextViewInViewController from applicationDidFinishLaunchingWithOptions, giving it the appropriate arguments. I think this code will do the trick. Cross your fingers!

EDIT: This code is not working. I got a SIGABRT message. I am considering giving up and just doing the old-fashioned way, however boring it is. Darn you, Xcode, for complicating a conceptually simple task!
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.