Home > Archives > 2011年2月 Archive

2011年2月 Archive

iPhone UIPickerView のラベルのフォント変更

  • Posted by: goron
  • 2011年2月 8日 15:37
  • iPad | iPhone

iPhoneでUIPickerViewを使ってシステムのフォント一覧を表示します。
せっかくフォントを表示するので、各ラベルのフォントも表示するフォントに合わせようと思ったら少々迷ったのメモします。

通常ですと、UIPickerViewのdelegateメソッドは

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
-(NSInteger)pickerView:(UIPickerView*)pictView numberOfRowsInComponent:(NSInteger)component
-(NSString*)pickerView:(UIPickerView *)pictView titleForRow:(NSInteger)row forComponent:(NSInteger)component

の3つですが、これですとNSStringしか返せません。
ですので、3つ目のメソッドは
- (UIView *)pickerView:(UIPickerView *)pictView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView*)view

を使うと、画像などUIView(UILabel)が使えます。

システムのフォント一覧を表示する場合は以下のようになります。


- (void)viewDidLoad {
[super viewDidLoad];
NSArray *family = [[UIFont familyNames] sortedArrayUsingSelector:@selector(compare:)];
familyNames = [[NSMutableArray alloc]init];
for ( id familyName in family ) {
NSArray* fonts = [[UIFont fontNamesForFamilyName:familyName] sortedArrayUsingSelector:@selector(compare:)];
for (NSString *name in fonts) {
[familyNames addObject:name];
}
}
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView*)pictView numberOfRowsInComponent:(NSInteger)component{
return [familyNames count];
}
/* フォント名のみ返す場合
-(NSString*)pickerView:(UIPickerView *)pictView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [NSString stringWithFormat:@"%@", [familyNames objectAtIndex:row]];
}
*/
//UILabelで返す場合
- (UIView *)pickerView:(UIPickerView *)pictView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView*)view{
NSString *fontname = [NSString stringWithFormat:@"%@", [familyNames objectAtIndex:row]];
UILabel* fontlabel = (UILabel*)view;
if (!fontlabel) {
fontlabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, ([pictView rowSizeForComponent:component].width - 8.0f), [pictView rowSizeForComponent:component].height)] autorelease];
fontlabel.adjustsFontSizeToFitWidth = YES;
fontlabel.backgroundColor = [UIColor clearColor];
}
fontlabel.text = fontname;
fontlabel.font = [UIFont fontWithName:fontname size:[UIFont labelFontSize]];
return fontlabel;
}

//ピッカーで取得 labelというUILabelへ選んだフォントを表示しています。
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSString *pickFont = [familyNames objectAtIndex:[thePickerView selectedRowInComponent:0]];
label.font = [UIFont fontWithName:pickFont size:[UIFont labelFontSize]];
label.text = pickFont;
}

- (void)dealloc {
[familyNames release];
[super dealloc];
}


サンプルソース


iPhoneでFacebookへの対応 (photo upload)

  • Posted by: goron
  • 2011年2月 1日 11:26
  • iPad | iPhone

iPhoneアプリで、写真共有サイトへのアップロードが必要になり
テストでいくつかプロジェクトを作成してみました。
facebookへの写真アップロードの部分でつまづいたのでメモです。

まず、facebookはiOS用のSDKは以下で配布されています。
https://github.com/facebook/facebook-ios-sdk

開発者登録、アプリ登録も必要です。
http://www.facebook.com/developers/createapp.php

組み込み方法はFacebookのdeveloperページにあります。
http://developers.facebook.com/docs/guides/mobile/

その他、参考にしたサイト。超簡単Facebook実装
http://www.slideshare.net/MakotoIto/facebook-6054768


僕は、自分のプロジェクトにsrcフォルダをコピーして、
facebook-ios-sdk.xcodeprojを立ち上げて、FBConnectフォルダを自分のプロジェクトへドロップしました。

SDKのサンプルフォルダにあるDemoAppを見れば大体作りが分かると思います。
自分のプロジェクトの場合は
1.自分のcontrollerに #import "FBConnect.h" する。
2.app idを入力する static NSString* kAppId = @"***********";(15桁の数字)
3.info.plistに URL Types > URL Shcemes に "fb" と 上記のapp idを加えた文字列を入れる

あと、忘れてはいけないのは、
自分のプロジェクトのappDelegateに下記を入れる。

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [[controller facebook] handleOpenURL:url];
}


これが入っていなくて、
The operation couldn't be completed. (facebookErrDomain error 101.)
というエラーで悩みました。
同じapp idで Demoでは動くのに、自分のプロジェクトで動かないことになっていました。
しかもInvalid API key となるので、謎でした。


写真アップロードに関して。
キャプションをつけて写真をアップロードしたい場合は、
paramsに@"caption"の項目を追加すればよいです。
グーグル先生では@"message"となっているのですが、これでは反映されません。


-(void)upload{
UIImage *img = [UIImage imageNamed:@"img.jpg"];
//キャプション入れる時
NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
img, @"picture",
@"ここにキャプションをいれる", @"caption", nil];
[_facebook requestWithMethodName:@"photos.upload" andParams:params andHttpMethod:@"POST" andDelegate:self];
}


あと、アプリを毎回ログインさせない方法は模索中です。。


Index of all entries

Home > Archives > 2011年2月 Archive

Profile

iPhone/iPad開発 web制作:平野百貨店
iPhone/iPad開発・web制作・映像制作をしている平野百貨店の店長個人の覚書です。
ご意見・ご感想などお問合せはコチラからどうぞ。

※最近ツッコミをもらうので一応書いておきます。ブログのタイトル「袖触れ合うも多少の縁」はわざとです。正確には「袖振り合うも多生の縁」が正解です。

Search
iPhone Apps






RSS
リンク
のこぎりそうの日記

Return to page top