iphone学习(原创+搜集)
网络上搜索,不断更新中…
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.减少图片在程序的缓存,尤其大图片画在更小的范围内时。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
- (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;
}
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:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
+ (UIImage *)myImageNamed:(NSString *)name{
name = [name substringToIndex:name.length - 4];
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"png"];
return [UIImage imageWithContentsOfFile:path];
}
name = [name substringToIndex:name.length - 4];
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"png"];
return [UIImage imageWithContentsOfFile:path];
}
5.计算String的Label范围 :
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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.
}
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];
01 |
G: 公元时代,例如AD公元 |
02 |
yy: 年的后2位 |
03 |
yyyy: 完整年 |
04 |
MM: 月,显示为1-12 |
05 |
MMM: 月,显示为英文月份简写,如 Jan |
06 |
MMMM: 月,显示为英文月份全称,如 Janualy |
07 |
dd: 日,2位数表示,如02 |
08 |
d: 日,1-2位显示,如 2 |
09 |
EEE: 简写星期几,如Sun |
10 |
EEEE: 全写星期几,如Sunday |
11 |
aa: 上下午,AM/PM |
12 |
H: 时,24小时制,0-23 |
13 |
K:时,12小时制,0-11 |
14 |
m: 分,1-2位 |
15 |
mm: 分,2位 |
16 |
s: 秒,1-2位 |
17 |
ss: 秒,2位 |
18 |
S: 毫秒 |
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)