你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> 分享一些iOS開辟適用的小技能

分享一些iOS開辟適用的小技能

編輯:IOS開發綜合

1.設置navigationbar title色彩

 UIColor *whiteColor = [UIColor whiteColor];
NSDictionary *dic = [NSDictionary dictionaryWithObject:whiteColor forKey:NSForegroundColorAttributeName];
[self.navigationController.navigationBar setTitleTextAttributes:dic];

2.獲得UIColor RGB

 UIColor *color = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:1.0];
const CGFloat *components = CGColorGetComponents(color.CGColor);
NSLog(@"Red: %f", components[0]);
NSLog(@"Green: %f", components[1]);
NSLog(@"Blue: %f", components[2]);
NSLog(@"Alpha: %f", components[3]);

3.修正textField的placeholder的字體色彩、年夜小

 [self.textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
[self.textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];

4.將color轉為UIImage

 - (UIImage *)createImageWithColor:(UIColor *)color {
 CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
 UIGraphicsBeginImageContext(rect.size);
 CGContextRef context = UIGraphicsGetCurrentContext();
 CGContextSetFillColorWithColor(context, [color CGColor]);
 CGContextFillRect(context, rect);
 UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
 return theImage;
}

5.加載啟動圖的時刻隱蔽statusbar

在info.plist中參加Status bar is initially hidden 設置為YES

6.獲得按鈕title的size

/**
 * 獲得按鈕title的size
 */
- (CGFloat)getBtnTitleWidth:(UIButton*)btn {
 CGSize titleSize = [btn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:btn.titleLabel.font}];
 return titleSize;
}

7.設置Status bar色彩

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -20, ScreenWidth, 20)];[view setBackgroundColor:COLOR_APP_MAIN];
[viewController.navigationController.navigationBar addSubview:view];

8.json轉dictionary,dictionary轉json

+ (NSString*)dictionaryToJson:(NSDictionary *)dic {

 NSError *parseError = nil;

 NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];

 return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

}
+(NSDictionary *)jsonToDic:(NSString*)jsonStr {
  NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
  NSError *err;
  NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
               options:NSJSONReadingMutableContainers
                error:&err];
  return dic;
}

9.能否許可推送

+(BOOL)isAllowedNotification{

 if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {

  UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];

  if(UIUserNotificationTypeNone != setting.types) {

   return YES;
  }
 }
 NSLog(@"不許可推送");
 return NO;
}

10.磁盤空間相干

+ (NSString *)memoryFormatter:(long long)diskSpace {
 NSString *formatted;
 double bytes = 1.0 * diskSpace;
 double megabytes = bytes / MB;
 double gigabytes = bytes / GB;
 if (gigabytes >= 1.0)
  formatted = [NSString stringWithFormat:@"%.2f GB", gigabytes];
 else if (megabytes >= 1.0)
  formatted = [NSString stringWithFormat:@"%.2f MB", megabytes];
 else
  formatted = [NSString stringWithFormat:@"%.2f bytes", bytes];
 NSLog(@"fotmatted=%@",formatted);

 return formatted;
}

+ (NSString *)totalDiskSpace {

 long long space = [[[[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil] objectForKey:NSFileSystemSize] longLongValue];
 return [self memoryFormatter:space];
}

+ (NSString *)freeDiskSpace {

 long long freeSpace = [[[[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil] objectForKey:NSFileSystemFreeSize] longLongValue];
 return [self memoryFormatter:freeSpace];
}

11.修正了leftBarButtonItem若何恢復體系側滑前往功效

 //設置署理
self.interactivePopGestureRecognizer.delegate = self;
#pragma mark - <UIGestureRecognizerDelegate>
//完成署理辦法:return YES :手勢有用, NO :手勢有效
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
 //當導航掌握器的子掌握器個數 年夜於1 手勢才有用
 return self.childViewControllers.count > 1;
}

或許用第三方 UINavigationController+FDFullscreenPopGesture

12.應用UIAppearance在某個狀況下設置色彩,字體等欠好使

只須要在對應的地位用layoutIfNeeded刷新一下便可以了

13.設置圓形圖片

/** 設置圓形圖片(放到分類中應用) */
- (UIImage *)cutCircleImage {
 UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
 // 獲得高低文
 CGContextRef ctr = UIGraphicsGetCurrentContext();
 // 設置圓形
 CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
 CGContextAddEllipseInRect(ctr, rect);
 // 裁剪
 CGContextClip(ctr);
 // 將圖片畫上去
 [self draWinRect:rect];
 UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
 return image;
}

14.假如在xib中有一個控件, 曾經明白設置尺寸了,輸入的frame也是對的, 然則顯示出來的後果紛歧樣(好比尺寸變年夜了), 假如是這類情形普通就是autoresizingMask主動伸縮屬性在弄鬼!

處理方法以下:

//xib的awakeFromNib辦法中設置UIViewAutoresizingNone停止清空

 - (void)awakeFromNib {
 self.autoresizingMask = UIViewAutoresizingNone;
}

15.經由過程圖片Data數據第一個字節 來獲得圖片擴大名

- (NSString *)contentTypeForImageData:(NSData *)data {
 uint8_t c;
 [data getBytes:&c length:1];
 switch (c) {
  case 0xFF:
   return @"jpeg";
  case 0x89:
   return @"png";  
  case 0x47:
   return @"gif";  
  case 0x49: 
  case 0x4D:
   return @"tiff";  
  case 0x52: 
   if ([data length] < 12) {
    return nil;
   }
   NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding];
   if ([testString hasprefix:@"RIFF"] && [testString hasSuffix:@"WEBP"]) {
    return @"webp";
   }
   return nil;
 }
 return nil;
}

16.用0補全的辦法

NSInteger count = 5;
//02代表:假如count缺乏2位 用0在最後面補全(2代表總輸入的個數)
NSString *string = [NSString stringWithFormat:@"%02zd",count];
//輸入成果是: 05
NSLog(@"%@", string);

總結

以上就是這篇文章的全體內容,願望本文中的這些小技能能給年夜家開辟IOS的時刻供給必定的贊助,假如有疑問年夜家可以留言交換。

【分享一些iOS開辟適用的小技能】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved