iphone学习(原创+搜集)

来源:互联网 发布:神圣计划软件 编辑:程序博客网 时间:2024/05/22 13:51

网络上搜索,不断更新中…

1.断点处查看变量内容:

po objc:输出[objc descripton];

print (int)[objc retainCount]:输出[objc retainCount)。 注:print [objc retainCount]不行。

print (CGRect)[view frame]:输出view.frame。  注:print [view frame] 或 print (CGRect)view.frame 不行。

2.修改UIAlertView背景:

theAlert.layer.contents = (id)[UIImageObjc CGImage];

3.减少图片在程序的缓存,尤其大图片画在更小的范围内时。

代码
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->- (UIImage *)rescaleImageToSize:(CGSize)size {
    CGRect rect
= CGRectMake(0.0,0.0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    [self drawInRect:rect]; 
// scales image to rect
    UIImage *resImage= UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
   
return resImage;
}


4.使用不长时间cache的UIImage:

代码
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->+ (UIImage *)myImageNamed:(NSString*)name{
    name
= [name substringToIndex:name.length-4];
    NSString
*path= [[NSBundle mainBundle] pathForResource:name ofType:@"png"];
   
return [UIImage imageWithContentsOfFile:path];
}


5.计算String的Label范围 :

代码
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->CGSize calcLabelSize(NSString *string, UIFont*font, NSInteger lines,float lineWidth) {
   
   
float lineHeight= [@"Fake line" sizeWithFont: font ].height;// Calculate the height of one line.
    if (string== nil ) {
       
return CGSizeMake(lineWidth, lineHeight);
    }
   
    NSMutableString
*tmpString= [[NSMutableString alloc] init];
    [tmpString setString:[
string stringByReplacingOccurrencesOfString:@"<br />" withString:@"\n"]];
   
   
int numLines= [tmpString sizeWithFont: font constrainedToSize: CGSizeMake(lineWidth, lineHeight*1000.0f) lineBreakMode: UILineBreakModeTailTruncation ].height / lineHeight;// Get the total height, divide by the height of one line to get the # of lines.
    [tmpString release];
   
   
if ( numLines> lines )
        numLines
= lines;// Set the number of lines to the maximum allowed if it goes over.
    numLines +=1;//rena add
    return CGSizeMake(lineWidth, (lineHeight*(float)numLines));// multiply the # of lines by the height of one line and return.
   
}


6.NSData格式化:

该格式可以指定以下内容:

NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"HH:mm 'on' EEEE MMMM d"];
NSString *newDateString = [outputFormatter stringFromDate:formatterDate];01G: 公元时代,例如AD公元
02yy: 年的后2位
03yyyy: 完整年
04MM: 月,显示为1-12
05MMM: 月,显示为英文月份简写,如 Jan
06MMMM: 月,显示为英文月份全称,如 Janualy
07dd: 日,2位数表示,如02
08d: 日,1-2位显示,如 2
09EEE: 简写星期几,如Sun
10EEEE: 全写星期几,如Sunday
11aa: 上下午,AM/PM
12H: 时,24小时制,0-23
13K:时,12小时制,0-11
14m: 分,1-2位
15mm: 分,2位
16s: 秒,1-2位
17ss: 秒,2位
18S: 毫秒


7.宏定义

// 是否高清屏#define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)// 是否iPad#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)// 是否模拟器#define isSimulator (NSNotFound != [[[UIDevice currentDevice] model] rangeOfString:@"Simulator"].location)
原创粉丝点击