iOS开发中的一些琐碎问题

1.UIButton的图片设置问题

设置按钮的backgroundImage 和设置按钮的image的区别在于:
设置BackgroundImage时图片拉伸显示,设置image则图片正常显示.
而且, 通过closeBtn.imageEdgeInsets = UIEdgeInsetsMake(top, left, bottom, right); 设置按钮图片缩进时, 只对image有效, 而对backgroundImage无效.

文字和图片调换位置:

moreButon.titleEdgeInsets = UIEdgeInsetsMake(0, -left, 0, 0);
moreButon.imageEdgeInsets = UIEdgeInsetsMake(0, left, 0, 0);

示例代码见1.按钮示例代码

2.监听UITextField的输入和删除

在UITextFieldDelegate代理方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
可以监听到文本框中字符的变化, 包括输入字符和删除字符操作.
其中的 range参数,可以根据range.length来判断是输入状态还是删除状态
range.length==0 输入时 range.length==1 删除时

#pragma mark - UITextFieldDelegate代理方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
     //手机号
     if (range.location >= 11) {
          //超过11位后不可输入
          return NO; 
      }
      //rang.length==0 输入时 rang.length==1 删除时
      if((range.location >= 10) && (range.length == 0)){
          //输入第11位时, 获取按钮变成可用状态
          [self setGetSmsBtnEnabled:YES];
      }else{
          //删除第11位时, 获取按钮变成不可用状态
          [self setGetSmsBtnEnabled:NO];
      }
}

附代码:


1.按钮示例代码
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //图片 73*73
    CGFloat buttonWH = 100;
    CGFloat buttonM = buttonWH + 12;
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 20, buttonWH, buttonWH)];
    [button setBackgroundImage:[UIImage imageNamed:@"icon.png"] forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor grayColor]];
    [self.view addSubview:button];
    
    UIButton *button1 = [[UIButton alloc]initWithFrame:CGRectMake(100+buttonM, 20, buttonWH, buttonWH)];
    [button1 setImage:[UIImage imageNamed:@"icon.png"] forState:UIControlStateNormal];
    [button1 setBackgroundColor:[UIColor grayColor]];
    [self.view addSubview:button1];
    
    UIButton *button2 = [[UIButton alloc]initWithFrame:CGRectMake(100, 20 + buttonM, buttonWH, buttonWH)];
    [button2 setBackgroundImage:[UIImage imageNamed:@"icon.png"] forState:UIControlStateNormal];
    [button2 setImageEdgeInsets:UIEdgeInsetsMake(30, 20, 30, 20)];
    [button2 setBackgroundColor:[UIColor grayColor]];
    [self.view addSubview:button2];
    
    UIButton *button3 = [[UIButton alloc]initWithFrame:CGRectMake(100+buttonM, 20 + buttonM, buttonWH, buttonWH)];
    [button3 setImage:[UIImage imageNamed:@"icon.png"] forState:UIControlStateNormal];
    [button3 setImageEdgeInsets:UIEdgeInsetsMake(30, 20, 30, 20)];
    [button3 setBackgroundColor:[UIColor grayColor]];
    [self.view addSubview:button3];
    
    UIButton *button5 = [[UIButton alloc]initWithFrame:CGRectMake(100, 20 + buttonM*2, 200, 80)];
    [button5 setImage:[UIImage imageNamed:@"icon.png"] forState:UIControlStateNormal];
    [button5 setTitle:@"设置" forState:UIControlStateNormal];
    [button5.titleLabel setFont:[UIFont systemFontOfSize:40]];
    [button5 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button5 setBackgroundColor:[UIColor grayColor]];
    [self.view addSubview:button5];
    
    UIButton *button6 = [[UIButton alloc]initWithFrame:CGRectMake(100, 20 + buttonM*3, 200, 80)];
    [button6 setImage:[UIImage imageNamed:@"icon.png"] forState:UIControlStateNormal];
    [button6 setTitle:@"设置" forState:UIControlStateNormal];
    [button6.titleLabel setFont:[UIFont systemFontOfSize:40]];
    [button6 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button6 setBackgroundColor:[UIColor grayColor]];
    [button6 setTitleEdgeInsets:UIEdgeInsetsMake(0, -180, 0, 0)];
    [button6 setImageEdgeInsets:UIEdgeInsetsMake(0, 110, 0, 0)];
    [self.view addSubview:button6];
}
3.UIButton文字左对齐
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
btn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
4.绘制指定尺寸和颜色的图片
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
    UIGraphicsBeginImageContext(size);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, CGRectMake(0.0f, 0.0f, size.width, size.height));
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
}
5.如下效果按钮
iOS开发中的一些琐碎问题_第1张图片
image.png
CGFloat titleEdgeInsetsL = 8;
CGFloat imageEdgeInsetsL = kScreenWidth - 32;
UIImage *shaixuanBtnImageSelected = [UIImage inBundleImageNamed:@"selected"];
UIImage *shaixuanBtnImageNormal = [self imageWithColor:[UIColor clearColor] size:shaixuanBtnImageSelected.size];
...
UIButton *zhongduanBtn = [self creatButtonWithTitle:@"终端" hasImage:NO andUnSelectedImage:nil andSelectedImage:nil];
[self.shaixuanView addSubview:zhongduanBtn];
zhongduanBtn.frame = CGRectMake(0, 0, kScreenWidth, cellBtnH);
[zhongduanBtn setImage:shaixuanBtnImageSelected forState:UIControlStateSelected];
[zhongduanBtn setImage:shaixuanBtnImageNormal forState:UIControlStateNormal];
zhongduanBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
zhongduanBtn.titleEdgeInsets = UIEdgeInsetsMake(0, titleEdgeInsetsL, 0, -titleEdgeInsetsL);
zhongduanBtn.imageEdgeInsets = UIEdgeInsetsMake(0, imageEdgeInsetsL, 0, -imageEdgeInsetsL);

你可能感兴趣的:(iOS开发中的一些琐碎问题)