记录我的iPhone历程

xzjxylophone 2012-03-15 06:02:46
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
...全文
850 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
xzjxylophone 2012-05-10
  • 打赏
  • 举报
回复
如何把带有文字的UILabel转成UIImage的图片保存下来
-(UIImage *) imageFromView: (UIView *)theView{

UIGraphicsBeginImageContext(theView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[theView.layerrenderInContext:context];
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;

}
我永不背弃 2012-04-28
  • 打赏
  • 举报
回复
输出一个对象的地址
NSLog(@"%p", &cardInfo)
xzjxylophone 2012-04-28
  • 打赏
  • 举报
回复
//获取CGFloat的时候,必须是2.0f / 3.0f
//如果是2 / 3的话,得到的结果为0
CGFloat scale = 2.0f / 3.0f;
NSLog(@"%f", scale);
xzjxylophone 2012-04-23
  • 打赏
  • 举报
回复

使用手势
http://blog.csdn.net/dongge_111/article/details/7350460
xzjxylophone 2012-04-23
  • 打赏
  • 举报
回复
CGAffineTransform 与 角度
从角度 转换为CGAffineTransform
CGAffineTransformMakeRotation(10.0);
从CGAffineTransform转换为角度
lastRotation = atan2f(newTransform.b, newTransform.a);
avi9111 2012-04-16
  • 打赏
  • 举报
回复
搞什么飞机? 这是抢分区

要记录,回学校抄笔记去了吧
xzjxylophone 2012-04-08
  • 打赏
  • 举报
回复
让一个界面 不接受用户的手势操作:
self.view.userInteractionEnabled = NO;
我永不背弃 2012-04-07
  • 打赏
  • 举报
回复
//设置选中Cell的响应事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
}

参考:
http://www.cnblogs.com/huangdongcheng/archive/2011/11/30/2268754.html
我永不背弃 2012-04-07
  • 打赏
  • 举报
回复
//设置选中Cell的响应事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
}

参考:
http://www.cnblogs.com/huangdongcheng/archive/2011/11/30/2268754.html
我永不背弃 2012-04-07
  • 打赏
  • 举报
回复
navigationController 跳转到指定的vc中

int len = self.navigationController.viewControllers.count;
UIViewController *vc = [self.navigationController.viewControllers objectAtIndex:len - 3];
[self.navigationController popToViewController:vc animated:YES];
xzjxylophone 2012-04-01
  • 打赏
  • 举报
回复
给UIViewController 增加背景图片
UIImage *image = [UIImage imageWithResource:@"backgroundImage" ofType:@"png"];
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
xzjxylophone 2012-03-22
  • 打赏
  • 举报
回复
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的硬盘容量和硬盘剩余容量
xzjxylophone 2012-03-21
  • 打赏
  • 举报
回复
了解了UIView 的 Frame 和bounds的区别
frame是对于整个ios设备的位置
而bounds记录的宽和高,默认的起始坐标为(0,0)
xzjxylophone 2012-03-20
  • 打赏
  • 举报
回复
关于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了
xzjxylophone 2012-03-19
  • 打赏
  • 举报
回复
又在此的测试了相关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);
xzjxylophone 2012-03-19
  • 打赏
  • 举报
回复
NSString *a7 = nil;
NSUInteger length7 = [a7 length];

NSString *a8 = @"";
NSUInteger length8 = [a8 length];
这个得到的结果是 a7==a8==0
也就是说,对于NSString 为空的情况下长度为0

想起用C#的时候,经常出现的错误是:未将对象引用实例化
对于OC 来说依然能给空对象发送消息
xzjxylophone 2012-03-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 darkdong 的回复:]

"汉字2个字节,英文以及符号1个字节",这就是GBK编码
所以只需计算GBK编码的字符串长度即可:
NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSUInteger length = [str lengthOfBytesUsing……
[/Quote]
经过测试这个是正确的。
谢谢 学习了
巴依老爷 2012-03-16
  • 打赏
  • 举报
回复
"汉字2个字节,英文以及符号1个字节",这就是GBK编码
所以只需计算GBK编码的字符串长度即可:
NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSUInteger length = [str lengthOfBytesUsingEncoding:encoding];
zgycsmb 2012-03-16
  • 打赏
  • 举报
回复
很好,加油,
给点分吧,
念茜 2012-03-15
  • 打赏
  • 举报
回复
想要将一个NSString类型字符串获取的长度转换成类似ASCII编码的长度,如汉字2个字节,英文以及符号1个字节这个功能。由于使用[NSString length]方法调用获取的长度是一个中文和一个英文都是一个字节,而使用

[NSString lengthOfBytesUsingEncoding:NSASCIIStringEncoding] 方法无法识别中文编码,真是令人揪心。

于是想获得一个char*类型的字符串,然后自己遍历一遍,将它整理为类似ASCII编码的格式,这里要用到

[NSString cStringUsingEncoding:NSUnicodeStringEncoding]函数获得一个const char*指针,然后对这个字符串进行遍历,遇/0就跳过,否则length+1。


感谢分享!

29,028

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧