值得收藏的iOS开发常用代码块

2020-01-18 16:10:45丽君

遍历可变数组的同时删除数组元素


NSMutableArray *copyArray = [NSMutableArray arrayWithArray:array];  
NSString *str1 = @“zhangsan”; 
for (AddressPerson *perName in copyArray) { 
  if ([[perName name] isEqualToString:str1]) { 
    [array removeObject:perName]; 
  } 
} 

获取系统当前语言


NSString *currentLanguage = [[NSLocale preferredLanguages] objectAtIndex:0];
NSLog(@"currentlanguage = %@",currentLanguage);

if ([currentLanguage containsString:@"zh-Hans"]) {
  NSLog(@"zh-Hans简体中文");
}else if ([currentLanguage containsString:@"zh-Hant"]) {
  NSLog(@"zh-Hant繁体中文");
}

UITableView的Group样式下顶部空白处理


UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.1)];
self.tableView.tableHeaderView = view;

UITableView的plain样式下,取消区头停滞效果


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
  CGFloat sectionHeaderHeight = sectionHead.height;
  if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView;.contentOffset.y>=0)
  {
    scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
  }
  else if(scrollView.contentOffset.y>=sectionHeaderHeight)
  {
    scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
  }
}

获取某个view所在的控制器


- (UIViewController *)viewController
{
 UIViewController *viewController = nil; 
 UIResponder *next = self.nextResponder;
 while (next)
 {
  if ([next isKindOfClass:[UIViewController class]])
  {
   viewController = (UIViewController *)next;   
   break;  
  }  
  next = next.nextResponder; 
 } 
  return viewController;
}

两种方法删除NSUserDefaults所有记录


//方法一
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];


//方法二
- (void)resetDefaults
{
  NSUserDefaults * defs = [NSUserDefaults standardUserDefaults];
  NSDictionary * dict = [defs dictionaryRepresentation];
  for (id key in dict)
  {
    [defs removeObjectForKey:key];
  }
  [defs synchronize];
}

打印系统所有已注册的字体名称


void enumerateFonts()
{
  for(NSString *familyName in [UIFont familyNames])
  {
    NSLog(@"%@",familyName);        
    NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];    
    for(NSString *fontName in fontNames)
    {
      NSLog(@"t|- %@",fontName);
    }
  }
}

获取图片某一点的颜色


- (UIColor*) getPixelColorAtLocation:(CGPoint)point inImage:(UIImage *)image
{

  UIColor* color = nil;
  CGImageRef inImage = image.CGImage;
  CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage];

  if (cgctx == NULL) {
    return nil; /* error */
  }
  size_t w = CGImageGetWidth(inImage);
  size_t h = CGImageGetHeight(inImage);
  CGRect rect = {{0,0},{w,h}};

  CGContextDrawImage(cgctx, rect, inImage);
  unsigned char* data = CGBitmapContextGetData (cgctx);
  if (data != NULL) {
    int offset = 4*((w*round(point.y))+round(point.x));
    int alpha = data[offset];
    int red = data[offset+1];
    int green = data[offset+2];
    int blue = data[offset+3];
    color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:
         (blue/255.0f) alpha:(alpha/255.0f)];
  }
  CGContextRelease(cgctx);
  if (data) {
    free(data);
  }
  return color;
}