一,效果圖。 二,工程圖。 三,代碼。 RootViewController.h RootViewController.m Utils.h Utils.m
一,效果圖。
二,工程圖。
三,代碼。
RootViewController.h
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
@end
RootViewController.m
#import "RootViewController.h"
#import "Utils.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//初始化背景圖
[self addView];
//初始化button
[self initButton];
}
#pragma -mark -functions
//初始化背景圖
-(void)addView
{
self.title=@"UIFont";
}
//初始化button
-(void)initButton
{
UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(50, 100, 100, 50)];
[rightButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
rightButton.backgroundColor=[UIColor redColor];
//給按鈕上的字設置大小
[Utils setDefaultFont:rightButton size:20];
[rightButton setTitle:@"完成" forState:UIControlStateNormal];
[self.view addSubview:rightButton];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
Utils.h
#import <Foundation/Foundation.h>
@interface Utils : NSObject
+ (void)setDefaultFont:(id)sender size:(float)size;
@end
Utils.m
#import "Utils.h"
@implementation Utils
//給某個按鈕上的數字設置大小
+ (void)setDefaultFont:(id)sender size:(float)size{
[sender setFont:[UIFont fontWithName:@"Helvetica" size:size]];
}
@end