我正在处理需要更改所有控件的动态字体大小的应用程序。
正如您在截图中看到的,我必须根据字体大小的百分比更改字体。
例如
100%
lbl.font=[UIFont systemFontOfSize:20.0f];
lbl1.font=[UIFont systemFontOfSize:30.f];
80%
lbl.font=[UIFont systemFontOfSize:16.0f];
lbl1.font=[UIFont systemFontOfSize:24.f];
50%
lbl.font=[UIFont systemFontOfSize:10.0f];
lbl1.font=[UIFont systemFontOfSize:15.f];
最好的方法是什么?
答案 0 :(得分:2)
您可以使用UIAppearance
。 Tutorial link
&安培;请看this。
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
的实施例强> 的
<强> 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];
修改强>
这取决于你。假设您应将百分比大小存储到NSUSerDefaults
或其他存储中。然后使用上面的代码,您可以根据您存储的百分比计算大小。知道了吗?
答案 1 :(得分:0)
回答我自己的问题。我不确定是最好的方法。
枚举用于不同的字体大小
typedef enum {
UIFontCategoryExtraSmall,
UIFontCategorySmall,
UIFontCategoryMedium,
UIFontCategoryLarge,
UIFontCategoryExtraLarge
} UIFontCategory;
#define FONT_SIZE_KEY @"fontsize"
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];
}
使用自定义字体类
lbl.font=[UIFont preferredFontSizeWithMaxFontSize:16.0f];
lbl1.font=[UIFont preferredFontSizeWithMaxFontSize:24.f];