1:判断设备
//设备名称
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
2:枚举的运用
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
3: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));
4: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);
}
5:带属性的字符串(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];
6:UIWebView加载PDF远端文件,并可以进行放大缩小简单的显示;
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
webView.scalesPageToFit = YES;
[self.view addSubview:webView];
NSString *path = @"http://temall.net:9090/html/reader.pdf"; //[[NSBundle mainBundle] pathForResource:@"SDK" ofType:@"pdf"]; //加载本地文件PDF
NSURL *url = [NSURL URLWithString:path]; //[NSURL fileURLWithPath:path]; //加载本地文件PDF
NSURLRequest *request = [NSURLRequest requestWithURL:url];
if (request) {
[webView loadRequest:request];
}