[关闭]
@SanMao 2015-08-06T00:35:28.000000Z 字数 3517 阅读 1406

UIButton&UILabel

UI


UIButton

  1. @implementation ViewController
  2. - (void)viewDidLoad {
  3. [super viewDidLoad];
  4. // Do any additional setup after loading the view, typically from a nib.
  5. // 在界面上添加一个UIButton按钮
  6. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  7. // UIButton *btn = [[UIButton alloc] init];
  8. btn.frame = CGRectMake(135, 200, 100, 30);
  9. // 正常状态下的图片
  10. [btn setImage:[UIImage imageNamed:@"like"] forState:UIControlStateNormal];
  11. // 高亮状态下的图片
  12. [btn setImage:[UIImage imageNamed:@"like_pressed"] forState:UIControlStateHighlighted];
  13. // 正常状态下的背景图
  14. [btn setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal];
  15. // 高亮状态下的背景图
  16. [btn setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"] forState:UIControlStateHighlighted];
  17. // 正常状态下的标题文字
  18. [btn setTitle:@"点赞" forState:UIControlStateNormal];
  19. // 高亮状态下的标题文字
  20. [btn setTitle:@"100" forState:UIControlStateHighlighted];
  21. // 标题文字颜色
  22. [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
  23. [self.view addSubview:btn];
  24. }
  1. // 监听按钮的点击(凡是继承自UIControl的对象都能通过addTarget方法监听事件)
  2.     [btn addTarget:self action:@selector(buttonclick) forControlEvents:UIControlEventTouchUpInside];
  3.     // 新建一个UISwitch
  4.     UISwitch *s = [[UISwitch alloc] init];
  5.     s.center = CGPointMake(150, 300);
  6.     [self.view addSubview:s];
  7.     [s addTarget:self action:@selector(switchClick:) forControlEvents:UIControlEventValueChanged];
  8. }
  9. - (void)switchClick:(UISwitch *)s {
  10.     NSLog(@"bianhua - %d",s.isOn);
  11. }

UILabel

  1. @property(nonatomic) NSInteger numberOfLines; // 默认是1行,行数设置为0时自动换行
  2. @property(nonatomic) NSTextAlignment textAlignment; // 对齐方式
  1. // Some convenience methods to create colors. These colors will be as calibrated as possible.
  2. // These colors are cached.
  3. + (UIColor *)blackColor; // 0.0 white
  4. + (UIColor *)darkGrayColor; // 0.333 white
  5. + (UIColor *)lightGrayColor; // 0.667 white
  6. + (UIColor *)whiteColor; // 1.0 white
  7. + (UIColor *)grayColor; // 0.5 white
  8. + (UIColor *)redColor; // 1.0, 0.0, 0.0 RGB
  9. + (UIColor *)greenColor; // 0.0, 1.0, 0.0 RGB
  10. + (UIColor *)blueColor; // 0.0, 0.0, 1.0 RGB
  11. + (UIColor *)cyanColor; // 0.0, 1.0, 1.0 RGB
  12. + (UIColor *)yellowColor; // 1.0, 1.0, 0.0 RGB
  13. + (UIColor *)magentaColor; // 1.0, 0.0, 1.0 RGB
  14. + (UIColor *)orangeColor; // 1.0, 0.5, 0.0 RGB
  15. + (UIColor *)purpleColor; // 0.5, 0.0, 0.5 RGB
  16. + (UIColor *)brownColor; // 0.6, 0.4, 0.2 RGB
  17. + (UIColor *)clearColor; // 0.0 white, 0.0 alpha
  1. + (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;
  1. [UIView beginAnimations:nil context:nil];
  2. [UIView setAnimationDuration:2.0];
  3. /* 需要执行动画的代码 */
  4. [UIView commitAnimations];
* 方式2:block式
  1. [UIView animateWithDuration:2.0 delay:1.0 options:kNilOptions animations:^{
  2. /* 需要执行动画的代码 */
  3. } completion:nil]
  4. // 1s后,再执行动画(动画持续2s)
  1. // 设置按钮内容的内边距(影响到imageView和titleLabel)
  2. @property(nonatomic) UIEdgeInsets contentEdgeInsets;
  3. // 设置titleLabel的内边距(影响到titleLabel)
  4. @property(nonatomic) UIEdgeInsets titleEdgeInsets;
  5. // 设置imageView的内边距(影响到imageView)
  6. @property(nonatomic) UIEdgeInsets imageEdgeInsets;
  1. // 只拉伸中间的1x1区域
  2. - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;
* iOS5开始
  1. - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets;
  2. - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode;

注意点

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注