zoukankan      html  css  js  c++  java
  • iOS开发基础知识--碎片3

    iOS开发基础知识--碎片3

     iOS开发基础知识--碎片3

    十二:判断设备

    复制代码
    //设备名称
    return [UIDevice currentDevice].name;
     
    //设备型号,只可得到是何设备,无法得到是第几代设备
    return [UIDevice currentDevice].model;
     
    //系统版本型号,如iPhone OS 
    return [UIDevice currentDevice].systemVersion;
     
    //系统版本名称,如6.1.3
    return [UIDevice currentDevice].systemName;
    
    
    //判断是否为iPhone
    #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
     
    //判断是否为iPad
    #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
     
    //判断是否为ipod
    #define IS_IPOD ([[[UIDevice currentDevice] model] isEqualToString:@"iPod touch"])
     
    //判断是否为iPhone5
    #define IS_IPHONE_5_SCREEN [[UIScreen mainScreen] bounds].size.height >= 568.0f && [[UIScreen mainScreen] bounds].size.height < 1024.0f
    复制代码

    十三:枚举的运用

    复制代码
    typedef enum  
    {  
        //以下是枚举成员  
        TestA = 0,  
        TestB,  
        TestC,  
        TestD  
    }Test;//枚举名称 
    
    亦可以如下定义(推荐:结构比较清晰),其中Test1为枚举的名称:
    
    typedef NS_ENUM(NSInteger, Test1)  
    {  
        //以下是枚举成员  
        Test1A = 0,  
        Test1B = 1,  
        Test1C = 2,  
        Test1D = 3  
    }; 
    
    枚举的定义还支持位运算的方式定义,向左移动几位,它是二进制(等于号后面必须等于1),转为十进制,如下:
    
    
    typedef NS_ENUM(NSInteger, Test)  
    {  
        TestA       = 1,      //1   1   1  
        TestB       = 1 << 1, //2   2   10      转换成 10进制  2  
        TestC       = 1 << 2, //4   3   100     转换成 10进制  4  
        TestD       = 1 << 3, //8   4   1000    转换成 10进制  8  
        TestE       = 1 << 4  //16  5   10000   转换成 10进制  16  
    };
    
    可以针对上面进行调用运算:
    
    Test tes=Testb;    //NSLog("%ld",tes);--->2
    Test newTes=(Testa|Testc);   //NSLog("%ld",newTes);--->5
    复制代码

    十四:IOS开发中的CGFloat、CGPoint、CGSize和CGRect

    1、数据类型:

    复制代码
    CGFloat: 浮点值的基本类型
    CGPoint: 表示一个二维坐标系中的点
    CGSize: 表示一个矩形的宽度和高度
    CGRect: 表示一个矩形的位置和大小
    
    typedef float CGFloat;// 32-bit
    typedef double CGFloat;// 64-bit
    
    struct CGPoint {
        CGFloat x;
        CGFloat y;
    };
    typedef struct CGPoint CGPoint;
    
    struct CGSize {
        CGFloat width;
        CGFloat height;
    };
    typedef struct CGSize CGSize;
    
    struct CGRect {
        CGPoint origin;
        CGSize size;
    };
    typedef struct CGRect CGRect;
    复制代码

    注意:CGRect数据结构的高度和宽度可以是负数。例如,一个矩形的原点是[0.0,0.0]和大小是[10.0,10.0]。这个矩形完全等同原点是[10.0,10.0]和大小是[-10.0,-10.0]的矩形。

    2、使用值来创建几何元素的方法

    复制代码
    CGPointMake
    CGRectMake
    CGSizeMake
    
    CGPoint CGPointMake (
       CGFloat x,
       CGFloat y
    );
    
    CGSize CGSizeMake (
       CGFloat width,
       CGFloat height
    );
    
    CGRect CGRectMake (
       CGFloat x,
       CGFloat y,
       CGFloat width,
       CGFloat height
    );
    
    
    CGFloat ten=10.0f;
    CGPoint point = CGPointMake(0.0f, 0.0f);
    CGSize size = CGSizeMake(10.0f, 10.0f);
    CGRect rect = CGRectMake(point.x, point.y, size.width, size.height);
    NSLog(@"ten: %f", ten);
    NSLog(@"point: %@", NSStringFromCGPoint(point));
    NSLog(@"size: %@", NSStringFromCGSize(size));
    NSLog(@"rect: %@", NSStringFromCGRect(rect));
    复制代码

    十五:ios动态获取UILabel的高度和宽度

    在使用UILabel存放字符串时,经常需要获取label的长宽数据,本文列出了部分常用的计算方法。

    1.获取宽度,获取字符串不折行单行显示时所需要的长度

    CGSize titleSize = [aString sizeWithFont:font constrainedToSize:CGSizeMake(MAXFLOAT, 30)];

    注:如果想得到宽度的话,size的width应该设为MAXFLOAT。

    2.获取高度,获取字符串在指定的size内(宽度超过label的宽度则换行)所需的实际高度.

    CGSize titleSize = [aString sizeWithFont:font constrainedToSize:CGSizeMake(label.frame.size.width, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];

    注:如果想得到高度的话,size的height应该设为MAXFLOAT。

    3.实际编程时,有时需要计算一段文字最后一个字符的位置,并在其后添加图片或其他控件(如info图标),下面代码为计算label中最后一个字符后面一位的位置的方法。

    复制代码
    CGSize sz = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    
    CGSize linesSz = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(label.frame.size.width, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
    
    if(sz.width <= linesSz.width) //判断是否折行
    {
       lastPoint = CGPointMake(label.frame.origin.x + sz.width, label.frame.origin.y);  
    }
    else  
    {  
       lastPoint = CGPointMake(label.frame.origin.x + (int)sz.width % (int)linesSz.width,linesSz.height - sz.height);  
    } 
    复制代码

     十六:带属性的字符串(NSMutableAttributedString)的使用

    复制代码
    1.创建对象
    
      NSString *original = @"今天你还好吗?";
    
      NSMutableAttributedString *attrTitle = [[NSMutableAttributedStringalloc] initWithString:original];
    
    2.设置颜色(NSForegroundColorAttributeName代表要设置颜色, value代表值, range 代表范围)
    
      /**
    
        其他设置:
    
          1.NSForegroundColorAttributeName //颜色
    
          2.NSFontAttributeName //字体
    
          3.NSBackgroundColorAttributeName //背景色
    
         //还有其他的很多的属性,可以自己去看苹果的API,这里不再详述
    
      */
    
      [attrTitle addAttribute:NSForegroundColorAttributeName value:[UIColorblueColor] range:NSMakeRange(0, 2)];
    
    3.添加到Label中
    
        UILabel *label = [[UILabel alloc] init];
    
        label.frame = CGRectMake(100, 100, 100, 40);
    
        [label setAttributedText:attrTitle];
    
        [self.view addSubview:label];
  • 相关阅读:
    c#中获取服务器IP,客户端IP以及Request.ServerVariables详细说明
    无废话MVC入门教程二[第一个小Demo]
    winform使用xml作为数据源
    SQL Server 2005为数据库设置用户名和密码的方法
    数据库 的版本为 661,无法打开。此服务器支持 655 版及更低版本。不支持降级路径。
    Visual Studio快速封装字段方法
    erp crm oa
    sqldbhelper
    OleDbHelper
    存储过程分页(3)
  • 原文地址:https://www.cnblogs.com/LiLihongqiang/p/5782705.html
Copyright © 2011-2022 走看看