ViewUtil.m 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // ViewUtil.m
  3. // JChat
  4. //
  5. // Created by HuminiOS on 15/7/23.
  6. // Copyright (c) 2015年 HXHG. All rights reserved.
  7. //
  8. #import "ViewUtil.h"
  9. @implementation ViewUtil
  10. + (UIImage *)colorImage:(UIColor *)c frame:(CGRect)frame {
  11. static NSMutableDictionary *imageCache;
  12. if (!imageCache) { imageCache = [[NSMutableDictionary alloc] init];}
  13. CGFloat w = frame.size.width;
  14. CGFloat h = frame.size.height;
  15. NSString *cache_key = [NSString stringWithFormat:@"%@_%d_%d",c, (int)w, (int)h];
  16. if (![imageCache objectForKey:cache_key]) {
  17. UIImage *img;
  18. CGRect rect=CGRectMake(0.0f, 0.0f, w, h);
  19. UIGraphicsBeginImageContext(rect.size);
  20. CGContextRef context = UIGraphicsGetCurrentContext();
  21. CGContextSetFillColorWithColor(context, [c CGColor]);
  22. CGContextFillRect(context, rect);
  23. img = UIGraphicsGetImageFromCurrentImageContext();
  24. UIGraphicsEndImageContext();
  25. imageCache[cache_key] = img;
  26. }
  27. return imageCache[cache_key];
  28. }
  29. + (UIView *)nib:(char *)nib {
  30. NSArray *nibs=[[NSBundle mainBundle] loadNibNamed:[NSString stringWithUTF8String:nib]
  31. owner:self
  32. options:nil];
  33. return [nibs objectAtIndex:0];
  34. }
  35. + (UIView *)nib:(char *)nib owner:(id)owner{
  36. NSArray *nibs=[[NSBundle mainBundle] loadNibNamed:[NSString stringWithUTF8String:nib]
  37. owner:owner
  38. options:nil];
  39. return [nibs objectAtIndex:0];
  40. }
  41. + (UITableViewCell *)table:(UITableView *)table nib:(char *)nib {
  42. UITableViewCell *cell = (UITableViewCell *)[table dequeueReusableCellWithIdentifier:[NSString stringWithUTF8String:nib]];
  43. if (cell == nil) {
  44. cell = (UITableViewCell *)[self nib:nib];
  45. }
  46. return cell;
  47. }
  48. + (void)table:(UITableView *)table registerNib:(char *)nib {
  49. [table registerNib:[UINib nibWithNibName:[NSString stringWithUTF8String:nib] bundle:nil] forCellReuseIdentifier:[NSString stringWithUTF8String:nib]];
  50. }
  51. @end