iOS 随笔

来源:互联网 发布:mblock软件下载 编辑:程序博客网 时间:2024/06/13 04:53

1.禁止UITextField可编辑  regionField.enabled = false; 


2.动画块不执行原因: 动画块viewdidload里面执行会直接展现动画结果没有动画过程 但放在viewdidappearviewwillappear里面执行就能够展现动画过程


3.创建Podfile文件,cd xxx(文件目录) 执行touch podfile 命令就会生成podfile文件 ,然后打开podfile文件 ,添加需要导入的类库

platform :ios, '7.0'
pod ‘JSONModel’
pod 'Masonry'


4.在 UIView 中有一个autoresizingMask的属性,它对应的是一个枚举的值(如下),属性的意思就是自动调整子控件与父控件中间的位置


5.JSONModel 替换解析属性名

+ (JSONKeyMapper *)keyMapper

{  return [[JSONKeyMapperalloc]initWithDictionary:@{@"id":@"id_s"}]; }


6. Git  删除远程分支 cd (iOS目录) git fetch -p


7. NSCalendar *calendar = [NSCalendarcurrentCalendar];// 取得当前用户的逻辑日历


9.在开发有时候需要将时间格式调整成自己希望的格式,这个时候可以用NSDateFormatter
//实例化一个NSDateFormatter对象
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//设定时间格式,这里可以设置成自己需要的格式
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//[NSDate date]可以获取系统当前时间
NSString *currentDate = [formatter stringFromDate:[NSDate date]];

NSLog(@”%@”,currentDate);


10.TaxtView 收回键盘 需实现代理 

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text  { if ([text isEqualToString:@"\n"]) {

   [textView resignFirstResponder];  

       return NO;

 return YES; 

}


11.GB2312、GBK和UTF-8都是一种字符编码,除此之外,还有好多字符编码。只是对于我们中国人的网站来说,用这三种编码 比较多。简单的说一下,为什么要用编码,在计算机内,储存文本信息用ASC II码,每一个字符对应着唯一的ASCII码。最初计算机是由美国发明的,他们也用的是键盘和上面的字母,所以他们的字符ASCII好解决。但是我们中国 的就不同了,每个汉字要对应唯一的ASCII码。这样,就出来了国家制定的字符编码标准:GB2312、GBK等。其他国家,其他语言也有他们对应的编码 标准。 GB 就是国标的意思,GB2312和GBK主要用于汉字的编码,而UTF-8是全世界通用的。


12.  self.navigationBar.translucent = NO;//决定导航栏是否是半透明的 YES/半透明。


13.自动计算字体高度

- (CGFloat)getTextHeight:(NSString *)text width:(CGFloat)width {

   NSMutableParagraphStyle *style = [[NSMutableParagraphStylealloc]init];

    style.lineSpacing =1.f;


   NSDictionary *dic =@{NSParagraphStyleAttributeName:style,

                         NSFontAttributeName:[UIFontsystemFontOfSize:13.f]};

   CGSize size = [textboundingRectWithSize:CGSizeMake(width,MAXFLOAT)

                                    options:NSStringDrawingUsesLineFragmentOrigin

                                 attributes:diccontext:nil].size;

   return size.height;

}


14.设置行间距

 NSMutableAttributedString *attribute = [[NSMutableAttributedStringalloc]initWithString:text];

 NSMutableParagraphStyle *style = [[NSMutableParagraphStylealloc]init];

 style.lineSpacing =3.f;

 NSRange range =NSMakeRange(0, text.length);

 [attributeaddAttribute:NSParagraphStyleAttributeNamevalue:stylerange:range];

 [promptContentsetAttributedText:attribute];


15.VC1 Push 进VC2 时移除VC1 

NSMutableArray *array = [AppNavigationController.viewControllersmutableCopy];

 [arrayremoveObject:self];

 [arrayaddObject:vc];

 [self.navigationControllersetViewControllers:arrayanimated:YES];


16.字符串编码

 NSString * encoding = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];


17.设置导航半透明

 self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0/255green:149.0/255blue:135.0/255alpha:1];

 self.navigationController.navigationBar.tintColor = [UIColor whiteColor];


18.设置ButtonTitle居左

[_replyUsersetContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];


19.九宫格

- (void)initButton {


   NSInteger itemCount =8;

   NSInteger itemWidth = ([UIScreenmainScreen].bounds.size.width -70.f) / 3;

    

   for (NSInteger i =0; i < itemCount; i ++) {

       NSInteger itemRow = i /3;

       NSInteger itemIndex = i %3;

        

       UIButton *button = [UIButtonnew];

        [buttonsetFrame:CGRectMake(20.f + itemIndex* (itemWidth +10.f), 70.f + itemRow *50.f , itemWidth,40.f)];

        button.backgroundColor = [UIColorredColor];

        [self.viewaddSubview:button];

    }

}


20.plist文件创建


+ (void)saveLoginInfo:(NSDictionary *)info {

    

   NSString *directory =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) [0];

   NSString *paths = [directorystringByAppendingPathComponent:@"1.plist"];

   DDLog(@"login info path -->> %@",paths);

   NSFileManager *fileManager = [NSFileManagerdefaultManager];

   if (![fileManagerfileExistsAtPath:paths]) {

         [fileManagercreateFileAtPath:pathscontents:nilattributes:nil];

         [infowriteToFile:pathsatomically:YES];

    }else {

       [infowriteToFile:pathsatomically:YES];

    }

}


+ (NSMutableDictionary *)readLoginInfo {

   NSString *directory =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)[0];

   NSString *paths = [directorystringByAppendingPathComponent:@"1.plist"];

   NSMutableDictionary *dataDict = [[NSMutableDictionaryalloc]initWithContentsOfFile:paths];

   return dataDict;

}


21.  

self.automaticallyAdjustsScrollViewInsets =NO;


22. 单例

+ (NSString *)initInstance {

    staticNSString *instance;

    staticdispatch_once_t predicate;

    dispatch_once(predicate, ^{

        instance = [[[NSStringclass]alloc]init];

    });

    return instance;

}


23. 写入文件到本地

+ (void)updateWriteToFile:(NSDictionary *)configFile {

    NSString *configPath = @"xxxxxx";

    NSData *configData = [NSJSONSerializationdataWithJSONObject:configFileoptions:NSJSONWritingPrettyPrintederror:nil];

    [configData writeToFile:configPathatomically:YES];

}


1 0