Skip to content

Instantly share code, notes, and snippets.

@setoh2000
setoh2000 / gist:1953420
Created March 1, 2012 21:39
UITableViewでスクロール停止時に表示されるセルを予想する iOS5以降
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
UITableView *tableView = (UITableView *)scrollView;
int n = tableView.frame.size.height / _cellHeight;
if ((int)tableView.frame.size.height % (int)_cellHeight) n++;
for (int i = 0; i <= n; i++) {
NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:CGPointMake(0, targetContentOffset->y + i * _cellHeight)];
@setoh2000
setoh2000 / gist:2286004
Created April 2, 2012 18:18
自分がModalViewControllerとして表示されたかどうかを調べる
- (BOOL)isModal
{
return (self == [self.navigationController.viewControllers objectAtIndex:0]);
}
@setoh2000
setoh2000 / gist:2340962
Created April 9, 2012 02:38
UITextFieldのカーソルを先頭に移動する方法 (Moving the cursor to the beginning of UITextField)
UITextPosition *newPos = [textField positionFromPosition:textField.beginningOfDocument offset:0];
textField.selectedTextRange = [textField textRangeFromPosition:newPos toPosition:newPos];
[textField setNeedsLayout];
@setoh2000
setoh2000 / gist:2354043
Created April 10, 2012 19:57
NSMutableOrderedSetを使って2つの配列(array1, array2)を重複なしに合体させてソートされた配列(array3)を得る方法
NSMutableOrderedSet *set = [NSMutableOrderedSet orderedSetWithArray:array1];
[set addObjectsFromArray:array2];
array3 = [set sortedArrayUsingComparator:^(NSString *obj1, NSString *obj2) {
return [obj1 compare:obj2];
}];
@setoh2000
setoh2000 / gist:2637433
Created May 8, 2012 17:10
現在表示しているviewControllerを別のviewControllerに差し替える
// 現在のviewControllerを閉じてanotherViewControllerに差し替える
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
[viewControllers removeLastObject];
[viewControllers addObject:anotherViewController];
[self.navigationController setViewControllers:viewControllers animated:YES];
@setoh2000
setoh2000 / gist:2884115
Created June 6, 2012 19:30
BlocksKitのselectとmatchの置き換え
//-------------------------------------------------------------------------
// MSBlocks.h
//-------------------------------------------------------------------------
@interface MSBlocks : NSObject
+ (NSArray *)array:(NSArray *)array select:(BOOL(^)(id))block;
+ (id)find:(NSArray *)array match:(BOOL(^)(id))block;
@end
//-------------------------------------------------------------------------
// MSBlocks.m
@setoh2000
setoh2000 / gist:2979659
Created June 23, 2012 19:36
USB経由でソースコピーしたらファイルの属性が変わってたのでこれで修正 (Mac)
# カレントディレクトリ以下のファイルは644, ディレクトリは755に統一
# (パーミッションを削る方向で)
find . -type f -print0 | xargs -0 chmod go-wx
find . -type f -print0 | xargs -0 chmod u-x
find . -type d -print0 | xargs -0 chmod go-w
@setoh2000
setoh2000 / gist:3264647
Created August 5, 2012 13:01
Mac版のEvernoteクライアントでSmartEverタグの付いたノートを作成するAppleScript
-- 2行目でタイトル、本文、ノートブックを指定してください
tell application "Evernote"
set note1 to create note title "Hello World!" with text "" notebook "TestNotebook"
set tag1 to tag "SmartEver"
assign tag1 to note1
open note window with note1
end tell
@setoh2000
setoh2000 / gist:3895173
Created October 15, 2012 20:25
Twitter.frameworkやSocial.frameworkのツイート画面でカーソルの初期位置を先頭にする
#import <Twitter/Twitter.h>
#import <Social/Social.h>
// TWTweetComposeViewControllerの場合 (for iOS5)
- (IBAction)tweet:(id)sender
{
if ([TWTweetComposeViewController canSendTweet]) {
// ツイート用の画面をを表示する
TWTweetComposeViewController* composeViewController = [[TWTweetComposeViewController alloc] init];
[composeViewController setInitialText:@"Hello World!"];
@setoh2000
setoh2000 / gist:4027038
Created November 6, 2012 19:48
Viewの周りに余白(margin)を付けて枠線を付ける
CGFloat margin = 3.0;
CGFloat w = aView.bounds.size.width;
CGFloat h = aView.bounds.size.height;
CALayer *borderLayer = [CALayer layer];
borderLayer.borderColor = [UIColor lightGrayColor].CGColor;
borderLayer.borderWidth = 1;
borderLayer.frame = CGRectMake(-margin, -margin, w + margin * 2, h + margin * 2);
[aView.layer addSublayer:borderLayer];