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

解决方案 »

  1.   

    想要将一个NSString类型字符串获取的长度转换成类似ASCII编码的长度,如汉字2个字节,英文以及符号1个字节这个功能。由于使用[NSString length]方法调用获取的长度是一个中文和一个英文都是一个字节,而使用[NSString lengthOfBytesUsingEncoding:NSASCIIStringEncoding] 方法无法识别中文编码,真是令人揪心。于是想获得一个char*类型的字符串,然后自己遍历一遍,将它整理为类似ASCII编码的格式,这里要用到[NSString cStringUsingEncoding:NSUnicodeStringEncoding]函数获得一个const char*指针,然后对这个字符串进行遍历,遇/0就跳过,否则length+1。感谢分享!
      

  2.   

    "汉字2个字节,英文以及符号1个字节",这就是GBK编码
    所以只需计算GBK编码的字符串长度即可:
    NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    NSUInteger length = [str lengthOfBytesUsingEncoding:encoding];
      

  3.   

        NSString *a7 = nil;
        NSUInteger length7 = [a7 length];
        
        NSString *a8 = @"";
        NSUInteger length8 = [a8 length];
    这个得到的结果是 a7==a8==0
    也就是说,对于NSString  为空的情况下长度为0想起用C#的时候,经常出现的错误是:未将对象引用实例化
    对于OC 来说依然能给空对象发送消息
      

  4.   

    又在此的测试了相关NSMutableArray的问题:
        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);
      

  5.   

    关于UILabel的 numberOfLines的问题:
    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了
      

  6.   

    了解了UIView 的 Frame 和bounds的区别
    frame是对于整个ios设备的位置
    而bounds记录的宽和高,默认的起始坐标为(0,0)
      

  7.   

        float totalSpace;
        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的硬盘容量和硬盘剩余容量
      

  8.   

    给UIViewController  增加背景图片
    UIImage *image = [UIImage imageWithResource:@"backgroundImage" ofType:@"png"];
    self.view.backgroundColor = [UIColor colorWithPatternImage:image];
      

  9.   

    navigationController 跳转到指定的vc中int len = self.navigationController.viewControllers.count;
        UIViewController *vc = [self.navigationController.viewControllers objectAtIndex:len - 3];
        [self.navigationController popToViewController:vc animated:YES];
      

  10.   

    //设置选中Cell的响应事件
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
    }参考:
    http://www.cnblogs.com/huangdongcheng/archive/2011/11/30/2268754.html
      

  11.   

    //设置选中Cell的响应事件
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
    }参考:
    http://www.cnblogs.com/huangdongcheng/archive/2011/11/30/2268754.html
      

  12.   

    让一个界面  不接受用户的手势操作:
    self.view.userInteractionEnabled = NO;
      

  13.   

    CGAffineTransform  与 角度
    从角度 转换为CGAffineTransform
    CGAffineTransformMakeRotation(10.0);
    从CGAffineTransform转换为角度
    lastRotation = atan2f(newTransform.b, newTransform.a);
      

  14.   


    使用手势
    http://blog.csdn.net/dongge_111/article/details/7350460
      

  15.   

                //获取CGFloat的时候,必须是2.0f / 3.0f
                //如果是2 / 3的话,得到的结果为0
                CGFloat scale = 2.0f / 3.0f;
                NSLog(@"%f", scale);
      

  16.   

    输出一个对象的地址
    NSLog(@"%p", &cardInfo)
      

  17.   

    如何把带有文字的UILabel转成UIImage的图片保存下来
    -(UIImage *) imageFromView: (UIView *)theView{
        
        UIGraphicsBeginImageContext(theView.frame.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [theView.layerrenderInContext:context];
        UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return theImage;
        
    }