码迷,mamicode.com
首页 > 其他好文 > 详细

改变UIButton上标题位置、文字颜色、背景色等

时间:2015-01-14 16:46:20      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, 200, 100)];
  
  //添加点击事件
  [button addTarget:self action:@selector(pushToSecond) forControlEvents:UIControlEventTouchUpInside];
  
  //整个按钮的背景色(无背景图片时有效果)
  button.backgroundColor = [UIColor greenColor];
  
  //添加背景图片
  [button setBackgroundImage:[UIImage imageNamed:@"01.png"] forState:UIControlStateNormal];
  
  //按钮标题
  [button setTitle:@"Hello World!" forState:UIControlStateNormal];
  
  //button上子控件的水平对齐方式
  button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  
  //button上子控件的垂直对齐方式
  button.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
  
  //内容容器的偏移,让文字向右偏移20像素
  button.contentEdgeInsets = UIEdgeInsetsMake(0,20, 0, 0);
  
  //设置标题的背景颜色
  button.titleLabel.backgroundColor = [UIColor purpleColor];
  
  //标题颜色
  [button setTitleColor:[UIColor yellowColor]forState:UIControlStateNormal];
  
  button.imageView.contentMode = UIViewContentModeScaleAspectFit;
  
  [self.view addSubview:button];
  [button release];

改变UIButton上标题位置、文字颜色、背景色等

标签:

原文地址:http://www.cnblogs.com/mona/p/4224251.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!