iOS 技巧

ios 常用的快捷代码CodeSnieept

2020-10-28  本文已影响0人  缘來諟夢
/*
 
---PString
@property(nonatomic,copy)NSString *

 
---动态增加cell
[_tempArr addObject:@"认捐物资"];
NSArray * indexPaths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:_tempArr.count-1 inSection:1]];
[self.gc_tableView beginUpdates];
[self.gc_tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft];
[self.gc_tableView endUpdates];
 
 
---关闭侧滑, 防止出现bug
 id traget = self.navigationController.interactivePopGestureRecognizer.delegate;
 UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:traget action:nil];
 [self.view addGestureRecognizer:pan];

 
---计算高度
CGRect contentRect1 =[markModel.briefing boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-93, MAXFLOAT) options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:FONT(14)} context:nil];
 
 
---图片适配
 _image.contentMode = UIViewContentModeScaleAspectFill;
 _image.clipsToBounds = YES;
 
 
---推出到指定控制器
 for (UIViewController *controller in self.navigationController.viewControllers) {
 if ([controller isKindOfClass:[AViewController class]]) {
     AViewController *A =(AViewController *)controller;
          [self.navigationController popToViewController:A animated:YES];
 
 
---系统Cell
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mineCell"];
 if (!cell) {
     cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mineCell"];
     cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
     cell.backgroundColor=[QCColor white];
     cell.textLabel.font=NORMALFONT;
     cell.selectionStyle=UITableViewCellSelectionStyleNone;
 }
 [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
 cell.imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"me_%@",_titleArr[indexPath.row]]];
 cell.textLabel.text=_titleArr[indexPath.row];
 return cell;
 
 
---指定cell刷新
 NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
 [self.gc_tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
 
 
---指定Section刷新
 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2];
 [self.gc_tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
 
 
 
---用户协议与隐私协议
 UITextView *contentTV = [[UITextView alloc] init];
     contentTV.font = FONT(10);
     //不要忘记设置frame!
     NSString *descText = @"登录即表示您已详细阅读并同意《中国移动认证服务条款》,开心果园《用户协议》与《隐私协议》";
     NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:descText];
     //设置被点击字体颜色
     contentTV.linkTextAttributes = @{NSForegroundColorAttributeName:RGB(255, 150, 102)};
     [attributedString addAttribute:NSLinkAttributeName
                              value:@"ProvidentFundshuaxin://"
                              range:[descText rangeOfString:@"《中国移动认证服务条款》"]];
     [attributedString addAttribute:NSLinkAttributeName
     value:@"yonghuxieyi://"
     range:[descText rangeOfString:@"《用户协议》"]];
     [attributedString addAttribute:NSLinkAttributeName
     value:@"yinsixieyi://"
     range:[descText rangeOfString:@"《隐私协议》"]];
     contentTV.attributedText = attributedString;
     contentTV.textColor = RGB(100, 100, 100);
     contentTV.delegate = self;
     contentTV.editable = NO;        //必须禁止输入,否则点击将弹出输入键盘
     contentTV.scrollEnabled = NO;
     [self.view addSubview:contentTV];
     [contentTV mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.mas_equalTo(self.view.mas_left).offset(30);
         make.width.mas_equalTo(SCREEN_WIDTH-60);
         make.bottom.mas_equalTo(self.view.mas_bottom).offset(-30);
         make.height.mas_equalTo(50);
     }];
 }
 #pragma mark ---- textView Delegate ----
 -(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
      if ([[URL scheme] isEqualToString:@"ProvidentFundshuaxin"]) {
          NSLog(@"富文本点击 服务条款");
      }else if ([[URL scheme] isEqualToString:@"yonghuxieyi"]) {
         NSLog(@"富文本点击 隐私政策");
      }
     else if ([[URL scheme] isEqualToString:@"yinsixieyi"]) {
        NSLog(@"富文本点击 隐私政策");
     }
         
     return YES;
 }
 
 
 ---指定圆角
 CGFloat radius = 15;
 UIRectCorner corner = UIRectCornerBottomRight|UIRectCornerBottomLeft; // 右上右左举例
 if (@available(iOS 11.0,*)) { // iOS11以上苹果提供了属性
     _upView.layer.cornerRadius = radius;
     _upView.layer.maskedCorners = (CACornerMask)corner;
 }else{ // iOS11已下用UIBezierPath设置
     UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:_upView.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
     CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
     maskLayer.frame = _upView.bounds;
     maskLayer.path = path.CGPath;
     _upView.layer.mask = maskLayer;
 }
 
 
 ---自定义cell
 static NSString *identifier = @"ArticlePricingCell";
 ArticlePricingCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
 if (cell == nil){
     cell = [[ArticlePricingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
 }
 
 
 ---Arr.count != 0
 if (partArr != nil && ![partArr isKindOfClass:[NSNull class]] && partArr.count != 0)
 {
     
 }
 
 
 ---Arr转String
 [nameArr componentsJoinedByString:@","]
 
 
 ---ArrayModel
 [self.dataList addObjectsFromArray:[NewsListDetailModel mj_objectArrayWithKeyValuesArray:data]];
 
 ---DicModel
 self.detailsModel = [PolicyDetailsModel mj_objectWithKeyValues:data];
 
 
 ---BUtton上图下字
 _serviceButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
 [_serviceButton setTitleEdgeInsets:UIEdgeInsetsMake(_serviceButton.imageView.frame.size.height+vnum ,-_serviceButton.imageView.frame.size.width, 0.0,0.0)];
 [_serviceButton setImageEdgeInsets:UIEdgeInsetsMake(-vnum, 0.0,0.0, -_serviceButton.titleLabel.bounds.size.width)];
 
 
 ---Buttton左字右图
 [_timebutton setTitle:@"全部时间" forState:UIControlStateNormal];
 [_timebutton setImage:[UIImage imageNamed:@"market_2"] forState:UIControlStateNormal];
 [_timebutton setTitleEdgeInsets:UIEdgeInsetsMake(0, -
  _timebutton.imageView.frame.size.width, 0, _timebutton.imageView.frame.size.width)];
 [_timebutton setImageEdgeInsets:UIEdgeInsetsMake(0, _timebutton.titleLabel.bounds.size.width, 0, - _timebutton.titleLabel.bounds.size.width)];
 
 
 ---CButton
 UIButton * cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
 cancelButton.frame = CGRectMake(100, 100, 100, 100);
 [cancelButton setTitle:@"保存" forState:UIControlStateNormal];
 [cancelButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
 [cancelButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
 [cancelButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
 [self.view addSubview:cancelButton];
 
 
 ---CellBlock
 @property (nonatomic, copy) void (^selectCallBack) (NSInteger index);
 
 
 ---CGFloat nameH
 CGFloat nameH =[self.partLabel getSpaceLabelHeight:_partLabel.text withWidh:SCREEN_WIDTH-50-90 font:13];
 
 ---CGSize titlesize
 CGSize titlesize = [_titleLabel.text sizeWithAttributes:@{NSFontAttributeName: SMALLFONT}];
 
 
 ---CreateTableView
 - (UITableView *)tableView{
     if (!_tableView) {
         _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
         _tableView.rowHeight = UITableViewAutomaticDimension;
         _tableView.estimatedRowHeight = 70;//这句必须写上值可以为自己估算的cell的高
         _tableView.delegate = self;
         _tableView.dataSource = self;
         _tableView.showsVerticalScrollIndicator = NO;
         _tableView.showsHorizontalScrollIndicator = NO;
         _tableView.estimatedRowHeight = 60;
         _tableView.estimatedSectionHeaderHeight = 0;
         _tableView.estimatedSectionFooterHeight = 0;
         _tableView.tableHeaderView = self.headerView;
         _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
         [_tableView registerNib:[UINib nibWithNibName:@"GreenRunRoundCell" bundle:nil] forCellReuseIdentifier:@"GreenRunRoundCell"];
         [_tableView registerNib:[UINib nibWithNibName:@"PolicyCommentsCell" bundle:nil] forCellReuseIdentifier:@"PolicyCommentsCell"];
         [_tableView registerNib:[UINib nibWithNibName:@"HomeListHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:@"HomeListHeaderView"];
     }
     return _tableView;
 }

 
 ---CreateXibCell
 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
     static NSString *identifer = @"LifeCardCell";//这里的cellID就是cell的xib对应的名称
     LifeCardCell *cell = (LifeCardCell *)[tableView dequeueReusableCellWithIdentifier:identifer];
     if(nil == cell) {
         NSArray *nib = [[NSBundle mainBundle] loadNibNamed:identifer owner:self options:nil];
         cell = [nib objectAtIndex:0];
     }
     [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
     [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
     cell.contentCell.text = _conArr[indexPath.row];
     return cell;
     
 }
 
 ---PUIImageView
 @property(nonatomic,strong)UIImageView *
 
 ---PArr
 @property(nonatomic,strong)NSMutableArray *
 
 ---PBool
 @property(nonatomic,assign)BOOL
 
 ---PButton
 @property(nonatomic,strong)UIButton *
 
 ---PFloat
 @property(nonatomic,assign)CGFloat
 
 ---PLabel
 @property(nonatomic,strong)UILabel *
 
 
---PMark
 #pragma mark --
 
 ---PNSInter
 @property(nonatomic,assign)NSInteger
 
 ---PStrong
 @property(nonatomic,strong)
 
 
 ---tableView滚动键盘消失
 - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
     if (scrollView.isTracking) {
         [self.view endEditing:YES];
     }
 }
 
 
 
 ---POP多级页面
 NSInteger index=[[self.navigationController viewControllers]indexOfObject:self];
 [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:index-3]animated:YES];
 
 ---PWindow
 - (void)setupWindow{
     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
     self.window.backgroundColor = [UIColor whiteColor];
     [self.window makeKeyAndVisible];
 }
*/
上一篇下一篇

猜你喜欢

热点阅读