宽容他人,放过自己。

快速创建

Posted on By anchoriteFili

删除字符串中的空格部分

+(NSMutableString *)cleanSpaceWithString:(NSString *)str {

    if (str == nil) {
        NSMutableString *newStr = [NSMutableString stringWithFormat:@""];
         return newStr;
    }else{
    NSMutableString *newStr = [NSMutableString stringWithFormat:@"%@",[str stringByReplacingOccurrencesOfString:@" " withString:@""]];
         return newStr;
    }
}

有下划线的button

#pragma mark 下划线创建
    UIButton *Button = [UIButton buttonWithType:UIButtonTypeCustom];
    Button.frame = CGRectMake(10, 10, 100, 30);
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc] initWithString:@"手机快速注册"]; //创建属性字符串
    NSRange StrRange = {0,[AttributedStr length]}; //设置下划线的长度
    [AttributedStr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:StrRange]; //添加下划线
    [Button setAttributedTitle:AttributedStr forState:UIControlStateNormal];
    [self.view addSubview:Button];

刷新动画效果——菊花的快速创建

菊花测试.zip

@property (nonatomic,strong) UIActivityIndicatorView *activityIndicatorView; //旋转菊花view
@property (nonatomic,strong) UIView *forwardView; //刷新是蒙版view

#pragma mark 添加动画效果 -- 点击事件
- (IBAction)click:(UIButton *)sender {
    
    [self.view addSubview:self.forwardView];
    self.activityIndicatorView.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2); //UIActivityIndicatorView大小不能改变,只能设置其center
    [self.activityIndicatorView startAnimating]; //开始动画
    [self.view addSubview:self.activityIndicatorView];
    //1.5秒后执行下面方法
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.activityIndicatorView stopAnimating]; //结束动画
        [self.activityIndicatorView removeFromSuperview]; //移除圆圈view
        [self.forwardView removeFromSuperview]; //移除蒙版view
    });
}

#pragma mark 创建蒙版view
- (UIView *)forwardView {
    if (!_forwardView) {
        _forwardView = [[UIView alloc] init];
        _forwardView.frame = [[UIScreen mainScreen] bounds];
        _forwardView.alpha = 0.5;
        _forwardView.backgroundColor = [UIColor grayColor];
    }
    return _forwardView;
}

#pragma mark 创建圆圈view
- (UIActivityIndicatorView *)activityIndicatorView {
    if (!_activityIndicatorView) {
        _activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        _activityIndicatorView.hidesWhenStopped = YES;
    }
    return _activityIndicatorView;
}

属性字符串的快速创建

属性字符串测试.zip

/**
     NSRange NSMakeRange (
     NSUInteger loc,
     NSUInteger len
     );
     其中第一个属性表示的是属性字符串改变属性的起始位置,3表示从第4个字符开始,2表示包括起始位置
     在内的后边的两个字符,而如果要算出后边空出几个字符,则需要用字符串总长度-起始位置数字-后边空
     出得位置的字符数,
     */

#pragma mark 颜色转换宏
#define RGB_COLOR(R, G, B) [UIColor colorWithRed:((R)/255.0f) green:((G)/255.0f) blue:((B)/255.0f) alpha:1.0f]

#pragma mark 页面自适应宏
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#define WIDTHADP self.view.frame.size.width/375
#define HEIGHTADP self.view.frame.size.height/667

#pragma mark 创建属性字符串
    NSMutableAttributedString *AttrStr = [[NSMutableAttributedString alloc] initWithString:@"排名第 4 名"];
    //改变字体的颜色
    [AttrStr addAttribute:NSForegroundColorAttributeName value:RGB_COLOR(112, 182, 35) range:NSMakeRange(3, AttrStr.length-3-1)]; //后边留一个字符不变
    //改变某个范围的字体的大小
    [AttrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(3, AttrStr.length-3-1)];
    NSLog(@"AttrStr.length = %lu",(unsigned long)AttrStr.length);
#pragma mark 进行全局的属性的设置
    UILabel *Label = [[UILabel alloc] initWithFrame:CGRectMake(WIDTHADP*77, HEIGHTADP*229.5, WIDTHADP*100, HEIGHTADP*16)];
    Label.font = [UIFont systemFontOfSize:14]; //特定区域外的字符串的属性的字号设置
    Label.textColor = [UIColor blackColor];
    Label.textAlignment = NSTextAlignmentRight;
    Label.attributedText = AttrStr;
    [self.view addSubview:Label];

tableViewCell中快速创建

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
    }
    return self;
}

button快速创建

#pragma mark 创建一个button
        UIButton *Button = [UIButton buttonWithType:UIButtonTypeCustom];
        Button.frame = CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>);
        Button.backgroundColor = [UIColor orangeColor];
        [Button setTitle:@"<#he#>" forState:<#(UIControlState)#>];
        [Button setTitleColor:<#(UIColor *)#> forState:UIControlStateNormal];
        [Button setTitleColor:<#(UIColor *)#> forState:UIControlStateHighlighted];
        [Button setBackgroundImage:<#(UIImage *)#> forState:UIControlStateNormal];
        [Button setBackgroundImage:<#(UIImage *)#> forState:UIControlStateHighlighted];
        [Button addTarget:self action:@selector(<#selector#>) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:Button];

#pragma mark 按钮点击方法
- (void)<#hehe#>:sender {
    
}

快速创建一个label

#pragma mark 创建一个label
        UILabel *Label = [[UILabel alloc] initWithFrame:CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)];
        Label.text = @"<#he#>";
        Label.textColor = [UIColor blackColor];
        Label.textAlignment = NSTextAlignmentRight; //字体对齐方向
        Label.backgroundColor = [UIColor orangeColor];
        Label.font = [UIFont systemFontOfSize:<#(CGFloat)#>];
        [self.contentView addSubview:Label];

快速创建一个textField

#pragma mark 创建一个textField
        UITextField *TextField = [[UITextField alloc] initWithFrame:CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)];
       
        TextField.textColor = [UIColor blackColor];
        TextField.placeholder = @"<#he#>";
        TextField.borderStyle = UITextBorderStyleRoundedRect; //设置输入框样式
        TextField.clearButtonMode = UITextFieldViewModeAlways; //总是显示清除按钮
        TextField.font = [UIFont systemFontOfSize:<#(CGFloat)#>];
        TextField.textAlignment = NSTextAlignmentRight;
        TextField.text = @"<#he#>";
        
        TextField.enabled = YES; //是否能够编辑
        TextField.clearsOnBeginEditing = NO; //是否开始输入的时候清空输入框内容
        TextField.secureTextEntry = NO; //输入框内容是否以原点方式显示 密码保护
//        TextField.keyboardType = UIKeyboardTypeNumberPad //设置弹出键盘的样式 数字键盘
        TextField.returnKeyType = UIReturnKeyDefault; //设置键盘右下角return按钮类型
//        TextField.inputView = myInputView; //自定义输入视图
        TextField.inputAccessoryView = myAccessoryView; //自定义键盘上方的辅助视图
        TextField.leftView = leftView; //输入框的左视图
        TextField.leftViewMode = UITextFieldViewModeAlways; //总是显示左视图
        TextField.rightView = rightView; //输入框的右视图
        TextField.rightViewMode = UITextFieldViewModeAlways; //总是显示右视图
        
        [self.contentView addSubview:TextField];

textFiled手势点击快速收起键盘

#pragma mark 添加点击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [self.view addGestureRecognizer:tap];

#pragma mark 手势点击事件
- (void)tap:(UITapGestureRecognizer *)sender {
    [self.searchTextFiled resignFirstResponder];
}

UIImageView的快速创建

#pragma mark 创建一个imageView
        UIImageView *ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)];
        ImageView.image = [UIImage imageNamed:<#(NSString *)#>];
        
        ImageView.userInteractionEnabled = NO; //图片是否支持交互
        UITapGestureRecognizer *ImageTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(<#selector#>)];
        [ImageView addGestureRecognizer:ImageTap];
        
        [self.contentView addSubview:ImageView];

#pragma mark 点击手势触发事件
- (void)<#selector#>:sender {
    
}