美文网首页iOS必看知识点
iOS常用知识点总结一

iOS常用知识点总结一

作者: 小鹏学长168 | 来源:发表于2019-01-10 15:19 被阅读3次
    • 1、iOS子线程更新UI到主线程的三种方法
    • 2、给self.view画一个类似self.layer.border的虚线边框
    • 3、iOS画小于1px的细线
    • 4、iOS 设置行距,并且计算有行距的文本高度
    • 5、iOS NSData 转 Byte
    • 6、PCH 绝对路径格式与相对路径
    • 7、mac系统如何显示或者隐藏隐形的文件夹
    • 8、Byte、char、NSData、NSString 相互转换
    • 9、NSMutableData 的拼接、删除、插入

    1、iOS子线程更新UI到主线程的三种方法

    // 1.NSThread
    [self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:NO];
    - (void)updateUI {    
    // UI更新代码    
    self.alert.text = @"Thanks!";
    }
    // 2.NSOperationQueue
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{  
      // UI更新代码    self.alert.text = @"Thanks!";    
    }];
    // 3.GCD
    dispatch_sync(dispatch_get_main_queue(), ^{   
    // UI更新代码   
    self.alert.text = @"Thanks!";
     });
    

    2、给self.view画一个类似self.layer.border的虚线边框

    // 给self.view画一个类似self.layer.border的虚线边框
    CAShapeLayer *border = [CAShapeLayer layer];
    border.strokeColor = SLColorLine.CGColor;
    border.fillColor = nil;
    border.path = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
    border.frame = self.bounds;
    border.lineWidth = 1.f;
    border.lineCap = @"square";
    border.lineDashPattern = @[@4, @2];
    [self.layer addSublayer:border];
    

    3、iOS画小于1px的细线

    CALayer *layer = view.layer;
    layer.borderColor = [UIColor whiteColor].CGColor;
    layer.borderWidth = (1.0 / [UIScreen mainScreen].scale / 2);
    

    4、iOS 设置行距,并且计算有行距的文本高度

    [text addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, text.length)];
     NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5];//调整行间距
    [text addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])];
    [self.topicDeatil.article.content boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:16]  ,NSParagraphStyleAttributeName : paragraphStyle} context:nil].size.height ;
    

    5、iOS NSData 转 Byte

    在iOS中NSData转Byte,见到最多的NSData转Byte的就是
        NSString *string = @"123456";
        NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
        Byte *testByte = (Byte *)[data bytes];
    
    但是这种方法常常是不可行的,这时候就要换下面的方法了,
        NSString *codeKey = @"123456";
        NSData *codeKeyData =  [codeKey dataUsingEncoding:NSUTF8StringEncoding];
        Byte codeKeyByteAry[codeKeyData.length];
        for (int i = 0 ; i < codeKeyData.length; i++) {
            NSData *idata = [codeKeyData subdataWithRange:NSMakeRange(i, 1)];
            codeKeyByteAry[i] =((Byte*)[idata bytes])[0];
        }
    

    6、PCH 绝对路径格式与相对路径

    PCH 绝对路径格式:/Users/wan/Desktop/项目名/项目名/PrefixHeader.pch,
          转化为相对路径,如下格式$(SRCROOT)/项目名/PrefixHeader.pch
    

    7、mac系统如何显示或者隐藏隐形的文件夹

    一:显示或者隐藏整个系统的隐形文件夹
       显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true
       隐藏Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool false
    二:显示或者隐藏某个目录下的隐形文件夹
       显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles YES
       隐藏Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles NO
    

    8、Byte、char、NSData、NSString 相互转换

    //1.1、NSData --> Byte
      NSString *testString = @"1234567890";
      NSData *testData = [testString dataUsingEncoding: NSUTF8StringEncoding];
      Byte *DataByte = (Byte *)[testData bytes];
       
    //1.2、Byte --> NSData
      Byte byte[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23};
      NSData *Bytedata = [[NSData alloc] initWithBytes:byte length:24];
      NSInteger byteLength2 = sizeof(byte2);
      NSLog(@"字节长度_byteLength2= %zd",byteLength2);//byteLength2= 24
      NSLog(@"---<2.2>testByte: %s", DataByte);
      NSLog(@"---<2.2>Bytedata: %@\n ", Bytedata);
    
    //2.1、 NSString 转化为 char*
      NSString *strr = @"Test转化123";
        const char * charA = [strr UTF8String];
      NSLog(@"charA = %s",charA);
    
    //2.2、char* 转化为 NSString
      NSString *charToString = [NSString stringWithUTF8String:charA];
      NSLog(@"charToString= %@ \n ",charToString);
    
    //3.1、:NSString --> NSData
        NSString * str1 = @"hello, world!";
        NSData * data = [str1 dataUsingEncoding:NSUTF8StringEncoding];
     
    //3.2、NSData --> NSString
        NSString * dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"---<2.1>data.bytes: %s", data.bytes);
        NSLog(@"---<2.1>dataString: %@\n ", dataString);
    
    

    9、NSMutableData 的拼接、删除、插入

        NSData *strData = [@"testdata" dataUsingEncoding:NSUTF8StringEncoding];
        NSMutableData *data = [NSMutableData data];
    
        [data appendData:strData];
        NSLog(@"data length= %ld",data.length);
        
        // delete
        [data replaceBytesInRange:NSMakeRange(0, 4) withBytes:NULL length:0];
        NSLog(@"data length= %ld",data.length);
        
        // insert
        [data replaceBytesInRange:NSMakeRange(0, 0) withBytes:strData.bytes length:strData.length];
        NSLog(@"data length= %ld",data.length);    
    }
    

    相关文章

      网友评论

        本文标题:iOS常用知识点总结一

        本文链接:https://www.haomeiwen.com/subject/dvrarqtx.html