Translate

2013年1月9日 星期三

UILabel auto size

我找過很多資料
發現大部份都是用比較複雜的方法去取得text的長度
再去幫UILabel從新設定尺寸大小
但是其實只需要關鍵的兩行代碼
就可以完成那些複雜的算式

UILabel *temp_lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 20)];
[temp_lab setText:@"ddddddddddddddddddddddddddddddddddddddddddddddddddd"];

//重點
[temp_lab setNumberOfLines:0];
[temp_lab sizeToFit];

2013年1月1日 星期二

NSFileModificationDate


NSDate *modificationDate = [[fileManager attributesOfItemAtPath:filename(檔案完整路徑) error:nil] objectForKey:NSFileModificationDate];

2012年12月26日 星期三

Init NSIndexPath

NSIndexPath *temp_IndexPath = [NSIndexPath indexPathForRow:lastIndexPath_row inSection:lastIndexPath_section]; 
其中lastIndexPath_row, lastIndexPath_section.形態為NSInteger 
主要是為了讓UITableView做edit有彈跳視窗所使用的

2012年12月20日 星期四

navigationBar setBackgroundImage


[naVC.navigationBar setBackgroundImage:[UIImage imageNamed:@"menubar.png"] forBarMetrics:UIBarMetricsDefault];

2012年12月19日 星期三

Xcode4.2 本地化多國語言Localizable跟AppleLanguages

1.再Supporting Files文件夾點右鍵,New File->iOS->Resource->Strings File取名為Localizable

2.選中Localizable.strings 再回Xcode->View->Utilites->File Inspector,再Localization中點擊+添加語言    比如中文英文日文

3.
Localizable.strings(English)
"test"=test";
Localizable.strings(Chinese)
"test="測試";

4.使用方法
[temp_lab setText:NSLocalizedString(@"test", @"測試用的")];
誇號裡第一個參數是要顯示的內容,與個Localozable.strings中的id對應
第二個參數是對第一個參數的註解,一般可以為空字串

另外也可以用程式取得使用者的語系
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
NSArray *languages = [defaults objectForKey:@"AppleLanguages"]; 
NSLog(@"%@", languages);

2012年12月7日 星期五

how to call shouldautorotatetointerfaceorientation

這個問題是我一個朋友問我的
覺得有可能需要紀錄下來

朋友的問題是
一開始假設機器是橫的
然後我拖動畫面~把開關設成NO~然後轉機器~這時候到了shouldAutorotateToInterfaceOrientation會因為我的布林值而不去旋轉畫面~但是事實上機器已經被設定成直式了
然後我放開後再去檢查機器的方向~這時候我會查到機器是直的~然後做直的layout~但是機器的畫面卻是橫的
所以畫面就整個不對了

最後結論就是
簡單來說就是怎麼去重新觸發shouldAutorotateToInterfaceOrientation

If you are target iOS 5, you can call

[UIViewController attempRotationToDeviceOrientation

which will can shouldAutorotateToInterfaceDeviceOrientation: with the device's current orientation and will rotate the interface if that method returns YES.

參考
http://stackoverflow.com/questions/9051192/iphone-change-orientation-manually

解決囉

2012年9月25日 星期二

NSArray compare 排序

其實一般NSArray的排序

NSArray *sortedArray = [temp_dic_dic sortedArrayUsingSelector:@selector(compare:)]; for(int i =0; i < [sortedArray count]; i++) { int x = [[sortedArray objectAtIndex:i] intValue]; NSLog(@"x is %d",x); }

但是我需要的是針對NSArray多個dictionary,每個dictionary都有個key值為"dddNumber",key值的形態為字串,如何依照NSString比對呢?

NSMutableArray *array = [[NSMutableArray alloc] init]; for (int i = 0; i < 10; i ++) { NSDictionary *aDict = [[NSDictionary alloc] initWithObjectsAndKeys:[NSString stringWithFormat:@"akdj%i", i], @"phaseNumber", nil]; [array addObject:aDict]; [aDict release]; } // 排序 NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"dddNumber" ascending:YES] autorelease]; NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; NSArray *sortedArray = [array sortedArrayUsingDescriptors:sortDescriptors];
是要用intValue排序的话。上面代码是不对的,上面是按照String来排序的。 如果要按照intValue排序,参见如下:

方法
NSComparisonResult compare(NSDictionary *firstDict, NSDictionary *secondDict, void *context) {NSLog(@"IN(%i, %i)", [[firstDict objectForKey:@"ddd"] intValue], [[secondDict objectForKey:@"ddd"] intValue]); if ([[firstDict objectForKey:@"ddd"] intValue] < [[secondDict objectForKey:@"ddd"] intValue]) return NSOrderedAscending; else if ([[firstDict objectForKey:@"ddd"] intValue] > [[secondDict objectForKey:@"ddd"] intValue]) return NSOrderedDescending; else return NSOrderedSame; } [array sortUsingFunction:compare context:NULL];
使用
[array sortUsingFunction:compare context:NULL];