美文网首页
IOS相关知识点

IOS相关知识点

作者: 江河_ios | 来源:发表于2018-01-09 15:36 被阅读0次

    相关文章 : 知识点

    面试的文章:相关的文章

    URL 有中文

    可以通过转码的方式进行解决

    urlStr= [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    1.图片的简单压缩

    1.//背景视图 (此种压缩有白边出现)

    1.//背景视图 (此种压缩有白边出现)

    UIImageView*bgIma = [[UIImageViewalloc] initWithFrame:BOUNDS];NSString*bgFile = [NSStringstringWithFormat:@"%@/%@",[[NSBundlemainBundle] resourcePath,@"zsBg.png"];

    //图片缓存处理

    UIImage*image = [[UIImagealloc] initWithContentsOfFile:bgFile];

    NSData*data =UIImageJPEGRepresentation(image,1.0); 

     bgIma.image = [UIImageimageWithData:data]; 

     [selfaddSubview:bgIma];

    2.封装方法 

     - (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize {

    // Create a graphics image contextUIGraphicsBeginImageContext(newSize);

    // Tell the old image to draw in this new context, with the desired

    // new size[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

    // Get the new image from the contextUIImage* newImage =UIGraphicsGetImageFromCurrentImageContext();

    // End the contextUIGraphicsEndImageContext();

    // Return the new image.

    return newImage; 

     } 

     ((UIImageView*)view).image = [selfimageWithImage:[UIImageimageNamed:self.notiArr[index]] scaledToSize:CGSizeMake(WIDTH-20, HEIGHT-64-10-180)] ;

    2.横竖屏详细相关(全局竖屏,特殊控制器横屏)**

        1.点项目 - Targets - General - Deployment Info ,如图

    如图所示

    2.在 appdelegate 头文件中添加属性/// 判断是否横竖屏@property(nonatomic,assign)BOOLallowRotation;

    3.在 appdelegate 实现文件中实现方法/* 横竖屏 */- (UIInterfaceOrientationMask)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window {if(self.allowRotation) {returnUIInterfaceOrientationMaskAllButUpsideDown; }returnUIInterfaceOrientationMaskPortrait; }

    4.在需要横屏的文件中,导入头文件#import"AppDelegate.h",

    添加如此代码

    /// 横竖屏相关

    AppDelegate *app =(AppDelegate *)[[UIApplicationsharedApplication] delegate]; app.allowRotation =YES;

    判断系统版本

    _判断 iOS 系统版本大于10_#define IOS_VERSION_10 (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_9_x_Max)?(YES):(NO) _判断 iOS系统具体版本_

    1.预编译文件(.pch文件)定义#define iPHONEType struct utsname systemInfo;uname(&systemInfo);引用 iPHONETypeNSString*platform = [NSStringstringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding];

    2.[UIDevicecurrentDevice].systemVersion.floatValue ,

    例如:if([UIDevicecurrentDevice].systemVersion.floatValue <7.0f) { Method newMethod = class_getInstanceMethod(self,@selector(compatible_setSeparatorInset:));// 增加Dummy方法class_addMethod(self,@selector(setSeparatorInset:), method_getImplementation(newMethod), method_getTypeEncoding(newMethod)); }

    十六进制转十进制

    - (NSString*)turn16to10:(NSString*)str {

    if(str.length>10) {     

           str =[str substringFromIndex:str.length-10];    

        }

    NSLog(@"字符=%@", str);

    unsignedlonglongresult =0;NSScanner*scanner = [NSScannerscannerWithString:str];        [scanner scanHexLongLong:&result];NSString*tempInt =[NSStringstringWithFormat:@"%llu", result];

    if(tempInt.length>7)

     {    

            tempInt =[tempInt substringFromIndex:tempInt.length-7];    

       }

    NSLog(@"数字=%@", tempInt);returntempInt;  

      }

    快速求和,最大值,最小值,平均值

    NSArray*array = [NSArrayarrayWithObjects:@"2.0",@"2.3",@"3.0",@"4.0",@"10",nil];CGFloatsum = [[array valueForKeyPath:@"@sum.floatValue"] floatValue];CGFloatavg = [[array valueForKeyPath:@"@avg.floatValue"] floatValue];CGFloatmax =[[array valueForKeyPath:@"@max.floatValue"] floatValue];CGFloatmin =[[array valueForKeyPath:@"@min.floatValue"] floatValue];

    Xcode 8 注释设置

        打开终端,命令运行: sudo /usr/libexec/xpccachectl

        然后必须重启电脑后生效

    NSDate与NSString的相互转化

    -(NSString*)dateToString:(NSDate*)date {

    // 初始化时间格式控制器

    NSDateFormatter*matter = [[NSDateFormatteralloc] init];

    // 设置设计格式

    [matter setDateFormat:@"yyyy-MM-dd hh:mm:ss zzz"];

    // 进行转换

    NSString*dateStr = [matter stringFromDate:date];

    return  dateStr;   

     }    

    -(NSDate*)stringToDate:(NSString*)dateStr 

    {

    // 初始化时间格式控制器

    NSDateFormatter*matter = [[NSDateFormatteralloc] init];

    // 设置设计格式

    [matter setDateFormat:@"yyyy-MM-dd hh:mm:ss zzz"];

    // 进行转换

    NSDate*date = [matter dateFromString:dateStr];

    return date;   

     }

    设置字体和行间距

    //设置字体和行间距

     UILabel * lable = [[UILabel alloc]initWithFrame:CGRectMake(50,100,300,200)]; 

    lable.text =@"大家好,我是Frank_chun,在这里我们一起学习新的知识,总结我们遇到的那些坑,共同的学习,共同的进步,共同的努力,只为美好的明天!!!有问题一起相互的探讨--438637472!!!"; lable.numberOfLines =0;

    lable.font = [UIFont systemFontOfSize:12];

    lable.backgroundColor = [UIColor grayColor]; 

    [self.view addSubview:lable]; 

    //设置每个字体之间的间距 

    //NSKernAttributeName 这个对象所对应的值是一个NSNumber对象(包含小数),作用是修改默认字体之间的距离调整,值为0的话表示字距调整是禁用的;

     NSMutableAttributedString * str = [[NSMutableAttributedString alloc]initWithString:lable.text attributes:@{NSKernAttributeName:@(5.0)}];

    //设置某写字体的颜色//NSForegroundColorAttributeName 设置字体颜色

    NSRange blueRange = NSMakeRange([[str string] rangeOfString:@"Frank_chun"].location, [[str string] rangeOfString:@"Frank_chun"].length);

     [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:blueRange]; 

    NSRange blueRange1 = NSMakeRange([[str string] rangeOfString:@"438637472"].location, [[str string] rangeOfString:@"438637472"].length);

    [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:blueRange1];

    //设置每行之间的间距

    //NSParagraphStyleAttributeName 设置段落的样式

    NSMutableParagraphStyle * par = [[NSMutableParagraphStyle alloc]init];

    [par setLineSpacing:20];

    //为某一范围内文字添加某个属性//NSMakeRange表示所要的范围,从0到整个文本的长度

    [str addAttribute:NSParagraphStyleAttributeName value:par range:NSMakeRange(0, lable.text.length)];

     [lable setAttributedText:str];

    效果图

    Label行间距

    -(void)test{    NSMutableAttributedString *attributedString =      [[NSMutableAttributedString alloc] initWithString:self.contentLabel.text];   

     NSMutableParagraphStyle *paragraphStyle =  [[NSMutableParagraphStyle alloc] init];    [paragraphStyle setLineSpacing:3];  

      //调整行间距         

     [attributedString addAttribute:NSParagraphStyleAttributeName                          value:paragraphStyle  range:NSMakeRange(0, [self.contentLabel.text length])];

        self.contentLabel.attributedText = attributedString;

    }

    判断一个点是否在一个区域,判断一个区域是否在一个区域 **

    1.CGRect和CGPoint对比 (判断一个点是否在一个区域)

    BOOLa =CGRectContainsPoint(_view.frame, point)

    2.CGRect和CGRect对比 (判断一个区域是否在一个区域)

    BOOLb =CGRectContainsRect(_view.frame,_btn.frame)

    3.CGPoint和CGPoint对比 (判断两个点是否相同)

    BOOLc =CGPointEqualToPoint(point1, point2);

    修改状态栏字体颜色

    只能设置两种颜色,黑色和白色,系统默认黑色设置为白色方法:

    (1)在plist里面添加Status bar style,值为UIStatusBarStyleLightContent(白色)或UIStatusBarStyleDefault(黑 色)(2)在Info.plist中设置UIViewControllerBasedStatusBarAppearance 为NO

     去掉导航栏下边的黑线

    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];

    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];

     修改pagecontrol颜色

    _pageControl.currentPageIndicatorTintColor=SFQRedColor;

    _pageControl.pageIndicatorTintColor=SFQGrayColor;

    如何代码实现点击http://www.kaka.com或400-800-400或message实现跳转safari,phone或message? 

    只需要在相应的代码中写入:  

    1、调用 电话phone 

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://4008008288"]];

    2、调用自带 浏览器 safari

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.abt.com"]];

    3、调用 自带mail

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://admin@abt.com"]];

    4、调用 SMS

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://800888"]];

    5,跳转到系统设置相关界面

    1 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=WIFI"]];

    其中,发短信,发Email的功能只能填写要发送的地址或号码,无法初始化发送内容,如果想实现内容的话,还需要更复杂一些,实现其各自的委托方法。

    若需要传递内容可以做如下操作:

    //加入:MessageUI.framework 

     #import 

    //实现代理:MFMessageComposeViewControllerDelegate 

    //调用sendSMS函数  

    //内容,收件人列表  

    - (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients  {  

    MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];  

    if([MFMessageComposeViewController canSendText])  {  

    controller.body = bodyOfMessage;    

    controller.recipients = recipients;  

    controller.messageComposeDelegate = self;  

         [self presentModalViewController:controller animated:YES];  

        }      

    }  

    // 处理发送完的响应结果  

    - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result  { 

      [self dismissModalViewControllerAnimated:YES];  

    if(result == MessageComposeResultCancelled) 

    NSLog(@"Message cancelled")  

    elseif(result == MessageComposeResultSent)  

    NSLog(@"Message sent")    

    else

    NSLog(@"Message failed")    

    }

    发送邮件的为:  

    //导入MFMailComposeViewController  

    #import 

    //实现代理:MFMailComposeViewControllerDelegate  

    //发送邮件 

    -(void)sendMail:(NSString *)subject content:(NSString *)content{  

    MFMailComposeViewController *controller = [[[MFMailComposeViewController alloc] init] autorelease];  

    if([MFMailComposeViewController canSendMail])  {  

            [controller setSubject:subject];  

            [controller setMessageBody:content isHTML:NO];    

           controller.mailComposeDelegate = self;  

            [self presentModalViewController:controller animated:YES];   

        }    

    }  

    //邮件完成处理  

    -(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{  

       [self dismissModalViewControllerAnimated:YES];  

    if(result == MessageComposeResultCancelled)  

    NSLog(@"Message cancelled"); 

    elseif(result == MessageComposeResultSent)  

    NSLog(@"Message sent");  

    else

    NSLog(@"Message failed");   

    }  

    默认发送短信的界面为英文的,解决办法为:在.xib 中的Localization添加一組chinese   

    程序中获取软件的版本号和app名称  

    应用程序的名称和版本号等信息都保存在mainBundle的infoDictionary字典中,用下面代码可以取出来。  

    NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];  

    NSString* versionNum =[infoDict objectForKey:@"CFBundleVersion"];//版本名称  

    NSString*appName =[infoDict objectForKey:@"CFBundleDisplayName"];//app名称 

    NSString* versionShortString = [infoDict objectForKey:@"CFBundleShortVersionString"];//标识应用程序发布版本号

    NSString*text =[NSString stringWithFormat:@"%@ %@",appName,versionNum,7versionShortString];

    此version 为工程info下的Bundle version字段值:value可以随意定义。

    CFBundleVersion,标识(发布或未发布)的内部版本号。这是一个单调增加的字符串,包括一个或多个时期分隔的整数。

    CFBundleShortVersionString  标识应用程序的发布版本号。该版本的版本号是三个时期分隔的整数组成的字符串。第一个整数代表重大修改的版本,如实现新的功能或重大变化的修订。第二个整数表示的修订,实现较突出的特点。第三个整数代表维护版本。该键的值不同于“CFBundleVersion”标识。

    图片里的 Version 对应的就是CFBundleShortVersionString (发布版本号 如当前上架版本为1.1.0  之后你更新的时候可以改为1.1.1)

    Build 对应的是 CFBundleVersion(内部标示,用以记录开发版本的,每次更新的时候都需要比上一次高 如:当前版本是11  下一次就要大于11 比如 12,13 ....10000)

    算法

    不用中间变量,用两种方法交换A和B的值

    // 1.中间变量

    void swap(int a, int b) {

      int temp = a;

      a = b;

      b = temp;

    }

    // 2.加法

    void swap(int a, int b) {

      a = a + b;

      b = a - b;

      a = a - b;

    }

    // 3.异或(相同为0,不同为1. 可以理解为不进位加法)

    void swap(int a, int b) {

      a = a ^ b;

      b = a ^ b;

      a = a ^ b;

    }

    求最大公约数

    /** 1.直接遍历法 */

    int maxCommonDivisor(int a, int b) {

        int max = 0;

        for (int i = 1; i <=b; i++) {

            if (a % i == 0 && b % i == 0) {

                max = i;

            }

        }

        return max;

    }

    /** 2.辗转相除法 */

    int maxCommonDivisor(int a, int b) {

        int r;

        while(a % b > 0) {

            r = a % b;

            a = b;

            b = r;

        }

        return b;

    }

    // 扩展:最小公倍数 = (a * b)/最大公约数

    相关文章

      网友评论

          本文标题:IOS相关知识点

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