Objective-C常用代码

CGRect的新写法:

CGRect frame = (CGRect){0,0,image.size};
CGRect sframe = (CGRect){0,0,10,10};
CGFloat x = CGRectGetMinX(frame);
CGFloat y = CGRectGetMinY(frame);
CGFloat width = CGRectGetWidth(frame);
CGFloat height = CGRectGetHeight(frame);
获取导航栏和状态栏的高度
CGRect rect = [[UIApplication sharedApplication] statusBarFrame];
状态栏的高度:
float status height =  rect.size.height;

导航栏的高度:
float navigationheight = self.navigationController.navigationBar.frame.size.height;

创建实例对象的新写法:

self.sendBtn = ({
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.layer.backgroundColor = [UIColor colorWithRed:0 green:123/255.0 blue:250.0/255 alpha:1.0].CGColor;
        btn.layer.cornerRadius = 2.0;
       
        [btn setTitle:@"发送" forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
        btn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
        [btn addTarget:self action:@selector(sendBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
        btn;
    });

弱引用

__weak typeof(self) weakSelf = self;

主线程

dispatch_async(dispatch_get_main_queue(), ^{

});

block使用

1)声明:
typedef void (^SubjectInputCallBack)(NSString * input);
2)定义:
@property(nonatomic,copy)SubjectInputCallBack inputCallback;
3)使用:
cell.inputCallback =^(NSString*input){
                eventModel.subject = input;
};
再如:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GPOperationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    cell.infoLabel.text = self.dataSource[indexPath.row];
    cell.cellActionCallBackBlock = ^(GPOperationTableViewCell *cell){
        //NSIndexPath *index = [tableView indexPathForCell:cell];
        //indexPath直接使用,它会对应相应cell的indexPath,不用重新根据cell来获取
       //若是对cell有删除操作的话,记得tableView reloadData一下,不然的话这些数据是没有进行相应的更新的,,或者是根据cell获取[tableView indexPathForCell:cell]也可以
        NSLog(@"indexPath:row = %ld, section = %ld",(long)indexPath.row,(long)indexPath.section);
    };
    return cell;
}

方法中添加block
- (void)textFieldDidChange:(NSString *)text complishBlock:(void(^)(NSInteger index))complishBlock;

谓词刷选数组:predicate

dataSource是array,存放model对象,model对象有select属性,
NSPredicate * predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"select == 1"]];
NSArray * select = [dataSource filteredArrayUsingPredicate:predicate];
if (select && select.count > 0) {
      return select;
}

创建indexpath:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.dataSoure.count - 1 inSection:0];

textField限制输入长度

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (range.location +range.length > textField.text.length) {
        return NO;
    }
    NSInteger lenght = textField.text.length + string.length - range.length;
    return lenght <= 24;
}

计算字符串size:

+ (CGSize)boundingALLRectWithSize:(NSString*)str Font:(UIFont*)font Size:(CGSize)size {
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
    NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
    [style setLineSpacing:2.0f];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0,str.length)];
    CGRect textRect = [str boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font,NSParagraphStyleAttributeName:style} context:nil];
    CGSize realSize = CGSizeZero;
    realSize = textRect.size;
    realSize.width = ceilf(realSize.width);
    realSize.height = ceilf(realSize.height);
    return realSize;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 7,338评论 1 14
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,387评论 30 472
  • iOS开发系列--网络开发 概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博、微信等,这些应用本身可...
    lichengjin阅读 9,190评论 2 7
  • 今天11月27号,阴,心情很不好,没有理由的不好,明天星期一,又需要工作了,迷茫期,挺迷茫,不单只是工作,还有生活...
    爱吃菠菜的比卡丘阅读 981评论 0 0
  • 第二章 3. 酒席正式开始,婚庆公司的策划做的那叫一个矫情。流程到了高潮,最会起哄的就属响河这桌。不过,响河还是自...
    金容与阅读 8,488评论 0 4