开发笔记

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

相关文章
相关标签/搜索