iOS项目总结的一些技巧

1.显示系统隐藏文件:

defaults write com.apple.finder AppleShowAllFiles YES

2.自己定义系统tableViewCell 的imageView的大小:

CGSize itemSize = CGSizeMake(40, 40);

UIGraphicsBeginImageContext(itemSize);

CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);

[cell.imageView.image drawInRect:imageRect];

cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

3.富文本编辑 实现不同文字的对齐:

NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc] initWithString:string];

[mutableString addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:NSMakeRange(1, 1)];

_label.attributedText = departString;

4.富文本编辑显示图片:

NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];

textAttachment.image = [UIImage imageNamd:];

label.attributedText = [NSAttributedString attributedStringWithAttachment:textAttachment];

5.获取当前视图的导航控制器:

- (UINavigationController*)getNavigationController

{

UINavigationController* nav = nil;

for (UIView* view = self.view; view; view = view.superview)

 {

UIResponder* responder = view.nextResponder;

if ([responder isKindOfClass:UIViewController.class]) 

{

UIViewController* vc = (UIViewController*)responder;

if (vc.navigationController) 

{

nav = vc.navigationController;

break;

}

}

}

return nav;

}

6.修复UITableView的分割线偏移的BUG

if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {

[self.tableView setSeparatorInset:UIEdgeInsetsZero];

}

if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {

[self.tableView setLayoutMargins:UIEdgeInsetsZero];

}

//此代理方法用来重置cell分割线

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{

if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {

[cell setSeparatorInset:UIEdgeInsetsZero];

}

if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {

[cell setLayoutMargins:UIEdgeInsetsZero];

}

}

#pragma mark 处理图片的压缩问题

- (UIImage*)compressedImage:(UIImage*)image

{

const CGFloat scaleSize = 0.5;//压缩比

UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize));

[image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];

UIImage* compressedImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return compressedImage;

}

7.iOS 时间比较

- (int)compareOneDay:(NSString*)oneDay withAnotherDay:(NSString*)anotherDay

{

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];

dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";

NSDate* dateA = [dateFormatter dateFromString:oneDay];

NSDate* dateB = [dateFormatter dateFromString:anotherDay];

NSComparisonResult result = [dateA compare:dateB];

if (result == NSOrderedDescending) {

//NSLog(@"Date1  is in the future");

return 1;

}

else if (result == NSOrderedAscending) {

//NSLog(@"Date1 is in the past");

return -1;

}

//NSLog(@"Both dates are the same");

return 0;

}

8.按要求截取相应的整数部分和小数部分

/**

*  数字截取相应的整数部分和小数部分

*

*  @param text    要截取的数据

*  @param num    整数部分位数

*  @param decimal 小数部分位数

*

*  @return 需要的字符串

*/

-(NSString *)textWithText:(NSString *)text addNumWithCount:(int )num addDecimalWithCount:(int)decimal

{    if ([text containsString:@"."])

{

// 只填了一个小数点  补0

if ([text rangeOfString:@"."].location==text.length-decimal)

{            for (int i=0; inum+decimal+1)

{

text = [text substringFromIndex:text.length-num-decimal-1];

}

}else

{

//全部整数不处理小数部分  需要处理可以在后面补0

if (text.length>num)

{

text = [text substringFromIndex:text.length-num];

}

}

return text;

}

8.时间戳转为时间(13位时间戳)

NSTimeInterval interval = [self.searchContentModel.createTime doubleValue]/1000.0;

NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"yyyy-MM-dd"];

你可能感兴趣的:(iOS项目总结的一些技巧)