我需要编程创建按钮,图像用于正常和突出显示的状态以及文本。我无法使用Interface Builder来构建它,因为我需要在UIScrollView
上创建按钮。这是我到目前为止的代码:以编程方式在UIButton上设置图像和文本
- (void)loadView {
CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];
scrollView=[[UIScrollView alloc] initWithFrame:fullScreenRect];
scrollView.contentSize=CGSizeMake(320,960);
UIImageView *tempImageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.jpeg"]];
UIImage * buttonImage = [UIImage imageNamed:@"contentlist_active.png"];
self.view=scrollView;
[scrollView addSubview:tempImageView2];
btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(22, 100, 277, 32);
[btn setImage:buttonImage forState:UIControlStateNormal];
[btn setTitle:@"hello world" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[scrollView addSubview:btn];
}
但是按钮上的文本没有显示。如果我将setImage
注释为button
,那么文本将完美显示,否则不显示。我可以同时拥有文字和图片吗?
你能告诉我如何对齐文本,左或右,如果我要放置阿拉伯文本为例。 – 2012-03-27 10:53:27