4

I am working on the application that needs to change dynamic font size of all the controls.

enter image description here

As you seen in the screenshot i have to change the font according to the percentage of font size.

For Example

For 100%

lbl.font=[UIFont systemFontOfSize:20.0f];
lbl1.font=[UIFont systemFontOfSize:30.f];

For 80%

lbl.font=[UIFont systemFontOfSize:16.0f];
lbl1.font=[UIFont systemFontOfSize:24.f];

For 50%

lbl.font=[UIFont systemFontOfSize:10.0f];
lbl1.font=[UIFont systemFontOfSize:15.f];

What's the Best way to do that?

Sourabh Sharma
  • 8,222
  • 5
  • 68
  • 78
Sunny Shah
  • 12,990
  • 9
  • 50
  • 86

2 Answers2

2

You can use UIAppearance. Tutorial link

& Take a look at this too.

Here's a list of classes that support this feature, in one way or the other.

UIActivityIndicatorView
UIBarButtonItem
UIBarItem
UINavigationBar
UIPopoverController
UIProgressView
UISearchBar
UISegmentedControl
UISlider
UISwitch
UITabBar
UITabBarItem
UIToolbar
UIView
UIViewController

Examples

UIActivityIndicatorView:

[[UIActivityIndicatorView appearance] setColor:[UIColor orangeColor]];

UINavigationBar:

[[UINavigationBar appearance] setTintColor:[UIColor brownColor]];
[[UINavigationBar appearanceWhenContainedIn:[MyCustomView class], nil] setTintColor:[UIColor blackColor]];

UIBarButtonItem:

[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
[[UIBarButtonItem appearanceWhenContainedIn:[MyCustomView class], nil] setTintColor:[UIColor magentaColor]];

UIProgressView:

[[UIProgressView appearance] setProgressTintColor:[UIColor yellowColor]];
[[UIProgressView appearance] setTrackTintColor:[UIColor greenColor]];

UISegmentedControl:

UIImage *segmentSelected = [[UIImage imageNamed:@"Segment_Selected.png"]
                        resizableImageWithCapInsets:UIEdgeInsetsMake(0, 12, 0, 12)];
UIImage *segmentUnselected = [[UIImage imageNamed:@"Segment_Unselected.png"]
                          resizableImageWithCapInsets:UIEdgeInsetsMake(0, 12, 0, 12)];

[[UISegmentedControl appearance] setBackgroundImage:segmentUnselected
                                               forState:UIControlStateNormal
                                             barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setBackgroundImage:segmentSelected
                                               forState:UIControlStateSelected
                                             barMetrics:UIBarMetricsDefault];

[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                        [UIColor magentaColor],UITextAttributeTextColor,
                                        [UIColor clearColor], UITextAttributeTextShadowColor,
                                        [NSValue valueWithUIOffset:UIOffsetMake(0, 0)], UITextAttributeTextShadowOffset,
                                        [UIFont fontWithName:@"Courier-Oblique" size:16.0], UITextAttributeFont, nil] forState:UIControlStateNormal];

[[UISegmentedControl appearance] setDividerImage:[UIImage imageNamed:@"SegmentedControl_Divider.png"]
                                 forLeftSegmentState:UIControlStateNormal
                                   rightSegmentState:UIControlStateNormal
                                          barMetrics:UIBarMetricsDefault];

UISlider:

[[UISlider appearance] setMinimumTrackImage:[UIImage imageNamed:@"Slider_Background.png"]
                                       forState:UIControlStateNormal];
[[UISlider appearance] setMaximumTrackImage:[UIImage imageNamed:@"Slider_Background.png"]
                                       forState:UIControlStateNormal];
[[UISlider appearance] setThumbImage:[UIImage imageNamed:@"Slider_Thumb.png"]
                                forState:UIControlStateNormal];

UISwitch:

[[UISwitch appearance] setOnTintColor:[UIColor redColor]];

UITabBar:

[[UITabBar appearance] setTintColor:[UIColor brownColor]];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor greenColor]];

UIToolBar:

[[UIToolbar appearance] setTintColor:[UIColor blueColor]];

UISearchBar:

[[UISearchBar appearance] setImage:[UIImage imageNamed:@"Search_Icon.png"]
                      forSearchBarIcon:UISearchBarIconSearch
                                 state:UIControlStateNormal];
[[UISearchBar appearance] setImage:[UIImage imageNamed:@"Search_Cross.png"]
                      forSearchBarIcon:UISearchBarIconClear
                                 state:UIControlStateNormal];
UIImage *searchBg = [UIImage imageNamed:@"Search_Background.png"];
searchBg = [searchBg stretchableImageWithLeftCapWidth:10 topCapHeight:10];
[[UISearchBar appearance] setBackgroundImage:searchBg];

EDIT:

It depends on you. Let's say you should store size of Percentage into NSUSerDefaults or other storage. Then by using above code you can calculate the size according to percentage you've stored. Got it?

Akshit Zaveri
  • 4,166
  • 6
  • 30
  • 59
0

Answer on my own question. i am not sure is best way to that.

Enum For the different Font size

  typedef enum {
    UIFontCategoryExtraSmall,
    UIFontCategorySmall,
    UIFontCategoryMedium,
    UIFontCategoryLarge,
    UIFontCategoryExtraLarge
} UIFontCategory;

#define FONT_SIZE_KEY @"fontsize"

Category Class of UIFont

@interface UIFont (CustomFonSize)

+(UIFont *)preferredFontSizeWithMaxFontSize:(CGFloat )fontSize;

@end



 @implementation UIFont (AvenirContentSize)

+ (UIFont *)preferredFontSizeWithMaxFontSize:(CGFloat )fontSize; {
    // choose the font size
    NSInteger currentFontSize=[[NSUserDefaults standardUserDefaults]integerForKey:FONT_SIZE_KEY];

    if (currentFontSize==UIFontCategoryExtraSmall) {
        fontSize = fontSize-8;

    } else if (currentFontSize==UIFontCategorySmall) {
        fontSize = fontSize-6;

    } else if (currentFontSize==UIFontCategoryMedium) {
        fontSize = fontSize-4;

    } else if (currentFontSize==UIFontCategoryLarge) {
        fontSize = fontSize-2;

    } else if (currentFontSize==UIFontCategoryExtraLarge) {
        fontSize = fontSize;

    }

    return [UIFont systemFontOfSize:fontSize];


}

Use of the Custom Font Class

lbl.font=[UIFont preferredFontSizeWithMaxFontSize:16.0f];
lbl1.font=[UIFont preferredFontSizeWithMaxFontSize:24.f];
Sunny Shah
  • 12,990
  • 9
  • 50
  • 86
  • Substracting is not the ideal way.. If you want to truly resize font size by percentage then you should probably use `0.8*fontSize` for 80% and `0.6*fontSize` for 60% and so on. [Note: 0.8 = 80%]. I hope u understood my explanation. – Akshit Zaveri Mar 05 '14 at 09:17
  • yaa i know but UIFontCategoryExtraSmall with 0.2 it will become too small. so i used Substracting – Sunny Shah Mar 05 '14 at 09:20
  • That depends on your application user. Any way.. It's good that u found solution. Don't forget to mark your answer after 2 days.. – Akshit Zaveri Mar 05 '14 at 09:21