更改按钮标签字体颜色和标题

时间:2016-03-28 10:14:35

标签: ios objective-c cocoa-touch

我想用自定义字体颜色和标题更改按钮标题标签。加载viewcontroller时,按钮显示为自定义1秒钟,然后显示为我在界面构建器中添加的

这是我的代码

edit_button.frame = CGRectMake(120, 40, 30, 30);
edit_button.titleLabel.text = @"";
edit_button.titleLabel.font = [UIFont fontWithName:@"FontAwesome" size:13.0f];
edit_button.titleLabel.textColor = [UIColor colorWithRed:0.278 green:0.608 blue:0.565 alpha:1];

3 个答案:

答案 0 :(得分:1)

对于UIButton,标题标签的属性设置如下:

    [edit_button setFrame:CGRectMake(x,y,width,height)];
    [edit_button setTitle:@"[]" forState:UIControlStateNormal];
    [edit_button setTitleColor:[UIColor colorWithRed:0.278 green:0.608 blue:0.565 alpha:1] forState:UIControlStateNormal];
    [edit_button.titleLabel setFont:[UIFont fontWithName:@"FontAwesome" size:13.0f]];

答案 1 :(得分:0)

尝试以这种方式设置按钮:

[button setTitle:@"Button title" forState:UIControlStateNormal];
[button setTitleColor:[UIColor colorWithRed:0.278 green:0.608 blue:0.565 alpha:1] forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont fontWithName:@"FontAwesome" size:13.0f]];

答案 2 :(得分:0)

[edit_button setTitle:@“[]”forState:UIControlStateNormal];在ViewController的 viewWillAppear:方法中

[self performSelector:@selector(loadButton) withObject:nil afterDelay:1.0f];

比实现方法 loadButton

-(void)loadButton
{
    edit_button.frame = CGRectMake(120, 40, 30, 30);
    [edit_button setTitle:@"TITLE" forState:UIControlStateNormal];
    edit_button.titleLabel.font = [UIFont fontWithName:@"FontAwesome" size:13.0f];
   [edit_button setTitleColor: [UIColor colorWithRed:71/255 green:155/255 blue:144/255 alpha:1 forState:UIControlStateNormal];//BE SURE TO DEVIDE BY 255]
   [self.view addSubview:edit_button]
}