iPad - 以编程方式将文本设置为按钮
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iPad - 以编程方式将文本设置为按钮相关的知识,希望对你有一定的参考价值。
我是这个iPad业务的新手,我正在基于XML文件的内容以编程方式在scrollview中创建一些按钮。我有这个代码:
float x = (SLIDER_ELEMENT_HEIGHT * i) + 20;
CGRect frame = CGRectMake(x, 0, SLIDER_ELEMENT_WIDTH, SLIDER_ELEMENT_HEIGHT);
UIButton *button = [[UIButton alloc] initWithFrame:frame];
UIColor *bgColor = [[UIColor alloc] initWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
button.backgroundColor = bgColor;
NSString *titleForButton = [NSString stringWithFormat: @"This is my title"];
[button setTitle:titleForButton forState:(UIControlStateNormal | UIControlStateApplication | UIControlStateHighlighted)];
UIColor *fgColor = [[UIColor alloc] initWithRed:0.5 green:0.5 blue:0.5 alpha:1.0];
[button setTitleColor:fgColor forState:(UIControlStateNormal | UIControlStateApplication | UIControlStateHighlighted)];
[button addTarget:self action:@selector(buttonMethod:) forControlEvents:(UIControlEvents)UIControlEventTouchDown];
[scrl_lastIssues addSubview:button];
现在,我追加的方法监听器工作正常,但按钮的文本从未出现......我做错了什么?
答案
尝试更改行:
[button setTitle:titleForButton forState:(UIControlStateNormal | UIControlStateApplication | UIControlStateHighlighted)];
至:
[button setTitle:titleForButton forState:UIControlStateNormal];
forState参数不能按照您当前的预期工作,它的工作方式如下:
[button setTitle:@"Normal" forState:UIControlStateNormal];
[button setTitle:@"Highlighted" forState:UIControlStateHighlighted];
另一答案
此外,如果您只设置UIControlStateNormal文本..它将是您通过按位OR分隔的所有控件状态的文本,我相信,这是您要搜索的功能。
另一答案
[button setTitle:@"Return" forState:UIControlStateNormal];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor orangeColor]];
另一答案
如果你想为所有州设置相同的标题,那么这样做:
[button setTitle:titleForButton forState:UIControlStateNormal];
[button setTitle:titleForButton forState:UIControlStateHighlighted];
以上是关于iPad - 以编程方式将文本设置为按钮的主要内容,如果未能解决你的问题,请参考以下文章
以编程方式将文本颜色设置为主要的 android textview