iphone中实现类似ASCII编码返回字符串长度类型的问题
- (int)stringLength:(NSString*)strtemp
{
int strlength = 0;
char* p = (char*)[strtemp cStringUsingEncoding:NSUnicodeStringEncoding];
for (int i=0 ; i<[strtemp lengthOfBytesUsingEncoding:NSUnicodeStringEncoding] ;i++) {
if (*p) {
p++;
strlength++;
}
else {
p++;
}
}
return strlength;
}详情参考:http://blog.csdn.net/arrui/article/details/5834542
- (int)stringLength:(NSString*)strtemp
{
int strlength = 0;
char* p = (char*)[strtemp cStringUsingEncoding:NSUnicodeStringEncoding];
for (int i=0 ; i<[strtemp lengthOfBytesUsingEncoding:NSUnicodeStringEncoding] ;i++) {
if (*p) {
p++;
strlength++;
}
else {
p++;
}
}
return strlength;
}详情参考:http://blog.csdn.net/arrui/article/details/5834542
所以只需计算GBK编码的字符串长度即可:
NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSUInteger length = [str lengthOfBytesUsingEncoding:encoding];
NSUInteger length7 = [a7 length];
NSString *a8 = @"";
NSUInteger length8 = [a8 length];
这个得到的结果是 a7==a8==0
也就是说,对于NSString 为空的情况下长度为0想起用C#的时候,经常出现的错误是:未将对象引用实例化
对于OC 来说依然能给空对象发送消息
NSMutableArray *array = nil;
NSString *a1 = [array objectAtIndex:0];
NSLog(@"%d", a1.length);//可以显示为0
array = [[NSMutableArray alloc] init];
a1 = [array objectAtIndex:0];//在这里出错了,index 0 beyond bounds for empty array'
NSLog(@"%d", a1.length);
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 210, 28)];
label.numberOfLines = 2;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:13];
label.textColor = [UIColor whiteColor];
label.text = labelText;
[titleView addSubview:label];当labelText比较长的时候,Label就是无法显示2行。
结果调试半天才知道:
把UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 210, 28)];中的28 改成30 就可以正确的显示2行了,
因为字体是13的时候,28 是无法容纳2行的,调成30就OK了
frame是对于整个ios设备的位置
而bounds记录的宽和高,默认的起始坐标为(0,0)
float totalFreeSpace;
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];
if (dictionary) {
NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemSize];
NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];
totalSpace = [fileSystemSizeInBytes floatValue];
totalFreeSpace = [freeFileSystemSizeInBytes floatValue];
NSLog(@"Memory Capacity of %f GB with %f GB Free memory available.", ((totalSpace/1024.0f)/1024.0f/1024.0f), ((totalFreeSpace/1024.0f)/1024.0f)/1024.0f);
} else {
NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %@", [error domain], [error code]);
}计算iphone的硬盘容量和硬盘剩余容量
UIImage *image = [UIImage imageWithResource:@"backgroundImage" ofType:@"png"];
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
UIViewController *vc = [self.navigationController.viewControllers objectAtIndex:len - 3];
[self.navigationController popToViewController:vc animated:YES];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
}参考:
http://www.cnblogs.com/huangdongcheng/archive/2011/11/30/2268754.html
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
}参考:
http://www.cnblogs.com/huangdongcheng/archive/2011/11/30/2268754.html
self.view.userInteractionEnabled = NO;
从角度 转换为CGAffineTransform
CGAffineTransformMakeRotation(10.0);
从CGAffineTransform转换为角度
lastRotation = atan2f(newTransform.b, newTransform.a);
使用手势
http://blog.csdn.net/dongge_111/article/details/7350460
//如果是2 / 3的话,得到的结果为0
CGFloat scale = 2.0f / 3.0f;
NSLog(@"%f", scale);
NSLog(@"%p", &cardInfo)
-(UIImage *) imageFromView: (UIView *)theView{
UIGraphicsBeginImageContext(theView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[theView.layerrenderInContext:context];
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}