在IOS程序中设置UIButton的字体大小

m_selectBox = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    [m_selectBoxsetFrame:CGRectMake(10, 5, APP_SCREEN_WIDTH - 20, TopHeight - 10)];

    [m_selectBoxsetTitle:@"全部"forState:UIControlStateNormal];

//    [m_selectBox setBackgroundImage:[Public imageWithName:@"btn_home" ofType:@"png"] forState:UIControlStateNormal];

    [m_selectBox.titleLabelsetTextAlignment:NSTextAlignmentCenter];

    [m_selectBox.titleLabelsetFont:[UIFontsystemFontOfSize:12]];

    [m_selectBoxsetContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];

    [m_selectBoxsetContentEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];

    [m_selectBoxaddTarget:selfaction:@selector(pressButtonChoose:) forControlEvents:UIControlEventTouchUpInside];

    [m_topButtonChooseaddSubview:m_selectBox];

 

    [self.viewaddSubview:m_topButtonChoose];

//设置按钮上的自体的大小

//[m_selectBox setFont:[UIFont systemFontOfSize:12]];    //这种可以用来设置字体的大小,但是可能会在将来的SDK版本中去除改方法

//应该使用

[m_selectBox.titleLabel setTextAlignment:NSTextAlignmentCenter];

    [m_selectBox.titleLabel setFont:[UIFont systemFontOfSize:12]];

//最后将按钮加入到指定视图superView

[self.view addSubview:m_topButtonChoose];

 

附:创建按钮的两种方法:

1、动态创建
btnfont = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnfont setFrame:CGRectMake(100, 10, 120, 40)];
[btnfont addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
[btnfont setTitle:@"字体" forState:UIControlStateNormal];
btnfont.backgroundColor=[UIColor clearColor];
[self.view addSubview:btnfont];
2、在xib文件中已经创建好,通过tag获取按钮
UIButton *testButton= (UIButton*)[self.view viewWithTag:100];
[testButton addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];

注册事件
-(void) test: (id) sender{

UIAlertView *av = [[[UIAlertView alloc] initWithTitle:@"ceshi" message:@"test11111" delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil] autorelease];

[av show];

}

猜你喜欢

转载自quankevin.iteye.com/blog/1912065