Objective-C常用代码
2017-03-22 本文已影响0人
东岳哥哥
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;
}