1.判斷時間web
-(NSString *)compareDate:(NSString*)dateString
{
NSTimeInterval seconds=24*60*60;
NSDate *today=[NSDate date];
NSDate *yesterday=[today dateByAddingTimeInterval:-seconds];
NSString *todayStr=[[today description]substringToIndex:10];
NSString *yesterdayStr=[[yesterday description]substringToIndex:10];
NSString *dateStr=[dateString substringToIndex:10];
if ([dateStr isEqualToString:todayStr]) {
return @"今天";
}else if ([dateStr isEqualToString:yesterdayStr]){
return @"昨天";
}else{
return dateStr;
}
}dom
2.經過圖片Data數據第一個字節 來獲取圖片擴展名ui
- (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;
}orm
3.xib控件尺寸顯示不正確圖片
(1) 若是在xib中有一個控件, 已經明確設置尺寸了,輸出的frame也是對的, 可是顯示出來的效果不同(好比尺寸變大了), 若是是這種狀況通常就是autoresizingMask自動伸縮屬性在搞鬼! 解決辦法以下:
//xib的awakeFromNib方法中設置UIViewAutoresizingNone進行清空
- (void)awakeFromNib ip
{ self.autoresizingMask = UIViewAutoresizingNone;
}
(2)若是你的控制器的view是用xib建立的, 當你拿到view的尺寸是不許確的, 在這裏咱們就須要經過[UIScreen mainScreen].bounds拿到尺寸, 可是storyboard的尺寸是準確的!開發
(3)xib中控件尺寸修改建議你們之後在viewDidLayoutSubviews計算尺寸
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
//在這裏計算尺寸
}字符串
4.設置圓形圖片
- (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;
}
5.## 與 @# 在宏裏面的使用
##是鏈接的做用, 即當使用上面的宏會把weak與輸入的type值鏈接起來
#define LRWeakSelf(type) __weak typeof(type) weak##type = type;
#的意思是緊跟着它的後面的標識符添加一個雙引號""
也就是說@#能夠代替@"" 那麼咱們之後開發就省事了, 不用再添加@""了!
#define LRToast(str) [NSString stringWithFormat:@"%@",@#str]
6.iOS中的AVSpeechSynthesizer能夠很輕鬆的實現實現文本到語音的功能,基本代碼以下:
self.speechSynthesizer = [[AVSpeechSynthesizer alloc] init];
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"FlyElephant"];
AVSpeechSynthesisVoice *voiceType = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
utterance.voice = voiceType;
//設置語速
utterance.rate *= 0.5;
//設置音量
utterance.volume = 0.6;
[self.speechSynthesizer speakUtterance:utterance];
AVSpeechUtterance能夠設置對應的語言,若是設置的語言不能識別文本不能生成語音播放,蘋果支持的語言以下:
* Arabic (ar-SA)
* Chinese (zh-CN, zh-HK, zh-TW)
* Czech (cs-CZ)
* Danish (da-DK)
* Dutch (nl-BE, nl-NL)
* English (en-AU, en-GB, en-IE, en-US, en-ZA)
* Finnish (fi-FI)
* French (fr-CA, fr-FR)
* German (de-DE)
* Greek (el-GR)
* Hebrew (he-IL)
* Hindi (hi-IN)
* Hungarian (hu-HU)
* Indonesian (id-ID)
* Italian (it-IT)
* Japanese (ja-JP)
* Korean (ko-KR)
* Norwegian (no-NO)
* Polish (pl-PL)
* Portuguese (pt-BR, pt-PT)
* Romanian (ro-RO)
* Russian (ru-RU)
* Slovak (sk-SK)
* Spanish (es-ES, es-MX)
* Swedish (sv-SE)
* Thai (th-TH)
* Turkish (tr-TR)
以上就是蘋果支持的語言編碼,固然你也能夠經過speechVoices遍歷對應的語言編碼:
NSArray *voice = [AVSpeechSynthesisVoice speechVoices];
for (AVSpeechSynthesisVoice *voiceModel in voice) {
NSLog(@"FlyElephant-%@",voiceModel);
}
6.播放系統聲音
#import <AVFoundation/AVFoundation.h>
AudioServicesPlaySystemSound(soundID);
7.自定義了一個返回按鈕.這樣系統默認的滑動返回手勢效果就沒有了的解決方法
咱們能夠B控制器中這樣作.
遵照 <UIGestureRecognizerDelegate>代理
-(void)addGesture
{
self.navigationController.interactivePopGestureRecognizer.delegate =(id)self;
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
self.currentShowVC = self;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return YES;
}
8.//不可變字符串的語法糖
NSString *string = @"hanjunqiang";
//可變字符串的語法糖
NSMutableString *mString = @"大愛中華".mutableCopy;//後綴不能丟
9.導航欄去除黑線
[self.navigationController.navigationBarsetBackgroundImage[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
10.tableview去除無數據時多餘的cell顯示
tableView.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];
11.判斷從哪一個界面推過來的
if ([[self.parentViewController class] isEqual:NSClassFromString(@"XXViewController")] )
12.經常使用的宏定義
#define WS(weakSelf) __weak __typeof(&*self)weakSelf = self;
#define ColorWithRGB(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0]
#defineRandomColor[UIColorcolorWithRed:arc4random()%255/255.0green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1.0]
#define KScreenWidth [[UIScreen mainScreen] bounds].size.width #define KScreenHeight [[UIScreen mainScreen] bounds].size.height