UIButton&UILabel

来源:互联网 发布:有趣的淘宝店铺 编辑:程序博客网 时间:2024/05/08 22:37

UIButton&UILabel

标签(空格分隔): UI


UIButton

  • UIButton的背景图片的拉伸只能通过代码的方式拉伸(resizableImageWithCapInsets:),不能通过storyboard(UIImageView却可以)

  • 使用

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

UILabel

@property(nonatomic) NSInteger numberOfLines; // 默认是1行,行数设置为0时自动换行@property(nonatomic) NSTextAlignment textAlignment; // 对齐方式
  • 默认情况下UILabel行数为1,修改行数为0时可自动换行
  • UILabel实现包裹文本内容(要使用自动布局)
    • 设置宽度约束(preferred)为 <= 固定值
    • 设置位置约束
    • 不用去设置高度约束
  • 界面中间的提醒内容
    • 指示器、HUD、遮盖、蒙板
    • 半透明的指示器如何实现?(加一个半透明的UIView)
      • 指示器的alpha = 1.0
      • 指示器的背景色是半透明的
  • 创建颜色
    • 直接创建对应的颜色
// Some convenience methods to create colors.  These colors will be as calibrated as possible.// These colors are cached.+ (UIColor *)blackColor;      // 0.0 white+ (UIColor *)darkGrayColor;   // 0.333 white+ (UIColor *)lightGrayColor;  // 0.667 white+ (UIColor *)whiteColor;      // 1.0 white+ (UIColor *)grayColor;       // 0.5 white+ (UIColor *)redColor;        // 1.0, 0.0, 0.0 RGB+ (UIColor *)greenColor;      // 0.0, 1.0, 0.0 RGB+ (UIColor *)blueColor;       // 0.0, 0.0, 1.0 RGB+ (UIColor *)cyanColor;       // 0.0, 1.0, 1.0 RGB+ (UIColor *)yellowColor;     // 1.0, 1.0, 0.0 RGB+ (UIColor *)magentaColor;    // 1.0, 0.0, 1.0 RGB+ (UIColor *)orangeColor;     // 1.0, 0.5, 0.0 RGB+ (UIColor *)purpleColor;     // 0.5, 0.0, 0.5 RGB+ (UIColor *)brownColor;      // 0.6, 0.4, 0.2 RGB+ (UIColor *)clearColor;      // 0.0 white, 0.0 alpha
  • 根据RGB组合创建颜色
+ (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;
  • 渐变动画
    • 方式1:头尾式
[UIView beginAnimations:nil context:nil];[UIView setAnimationDuration:2.0];/* 需要执行动画的代码 */[UIView commitAnimations];
* 方式2:block式
[UIView animateWithDuration:2.0 delay:1.0 options:kNilOptions animations:^{    /* 需要执行动画的代码 */} completion:nil]// 1s后,再执行动画(动画持续2s)
  • 按钮
    • 自定义按钮:调整内部子控件的frame
      • 方式1:实现titleRectForContentRect:imageRectForContentRect:方法,分别返回titleLabel和imageView的frame
      • 方式2:在layoutSubviews方法中设置
    • 内边距
// 设置按钮内容的内边距(影响到imageView和titleLabel)@property(nonatomic)          UIEdgeInsets contentEdgeInsets;// 设置titleLabel的内边距(影响到titleLabel)@property(nonatomic)          UIEdgeInsets titleEdgeInsets;// 设置imageView的内边距(影响到imageView)@property(nonatomic)          UIEdgeInsets imageEdgeInsets;
  • 图片拉伸
    • iOS5之前
// 只拉伸中间的1x1区域- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;
* iOS5开始
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets;- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode;

注意点

  • 任何一个控件都能容纳其他控件,但是在storyboard中添加后可能是同级关系,只有通过代码添加的才能容纳
  • 凡是继承自UIControl的控件,都可以通过addTarget:...方法来监听事件
0 0
原创粉丝点击