iphone学习(原创+搜集)

本文介绍了iOS开发中的实用技巧,包括断点处查看变量内容的方法、UIAlertView背景的修改、减少图片缓存的技术、UIImage的短时缓存使用、计算UILabel尺寸的代码及NSDateFormatter的格式化等。

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

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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值