-->
ラベル objective-C の投稿を表示しています。 すべての投稿を表示
ラベル objective-C の投稿を表示しています。 すべての投稿を表示

2015年12月19日土曜日

UILabel, UIView, UIButtonの角を丸くする

UILabelとUIViewの場合
 
UILabel *label =  [UILabel alloc]init];
label.clipsToBounds = true;
label.layer.cornerRadius = 3.0f;

UIView *view = [UIView alloc]init];
view.clipsToBounds = true;
view.layer.cornerRadius = 3.0f;
UIButtonの場合
 
UIButton *button = [UIButton alloc]init];
button.layer.cornerRadius = 3.0f;

2015年12月7日月曜日

継承したクラスのdelegateメソッドを表示

Xcodeを使ってて今更知ったこと

例えば、UITableViewを継承したカスタムクラスを作ったときに、
必要なdelegateメソッドを知る方法


「UITableViewDelegate」をcommandキーを押しながらクリック


必須のメソッドは
@required

オプションのメソッドは
@optional
で書いてあります。

2015年11月28日土曜日

新しいXcodeでカスタムセルを作ろうと…

久しぶりにアプリを作り始める
↓
UITableViewCellを継承したカスタムセル作るぞー
↓
 
Next(ぽち)
↓
ん…?なんか違うような…
 
Next(ぽち)
↓
えっ!!



正しくはこちら


↓

 



2014年10月13日月曜日

UINavigationBarのtitle、backButtonのフォントを変える

まず、「AppDelegate.m」の『- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions』に、
 
    UIFont *barButtonTitleFont = [UIFont fontWithName:@"mplus-2p-light" size:13];
    UIColor *barButtonTitleColor = [UIColor whiteColor];
    [[UIBarButtonItem appearance] setTitleTextAttributes:@{UITextAttributeFont:barButtonTitleFont, UITextAttributeTextColor:barButtonTitleColor} forState:UIControlStateNormal];
を記述。これで、BackButtonのフォントが変わる

次に、NavigationControllerの『ViewDidLoad』に、
    UILabel* label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 44)];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont fontWithName:@"mplus-2p-light" size:22.0f];
    label.textColor = [UIColor flatWhiteColor];
    label.text = @"タイトル";
    label.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = label;
を記述。これで、タイトルのフォントが変わる。

それはそうと、m+フォントってとても美しいよね

アプリ起動時に指定した画像を表示させる

アプリを起動した時に指定した画像を表示させる
(Twitterの公式クライアントアプリとかであるやつ)
  1. プロジェクト中にある「images.xcassets」を開いて、起動時に表示したい画像をドラッグアンドドロップで設定する。

    ちなみに、必要な画像サイズはinspectorに書いてある。


  2. 「info.plist」に「Status bar is initially hidden」 という項目を追加して、Boolean型のYESに設定する。


  3. 同じく、「View controller-based status bar appearance」という項目を追加して、Boolean型のNOに設定する。
     これで、起動時からステータスバーが表示されなくなる。


  4.  「AppDelegate.m」の-『 (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions』に、
    [NSThread sleepForTimeInterval:1.0];
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    を記述する。


    これで、起動から1秒間、設定した画像が表示される
    あとは、フェードアウトするアニメーションを追加したい・・・

2014年10月11日土曜日

インスタンスメソッドの定義方法

今更ながらObj-Cの文法まとめ
あってるかなあ?

・きっちりしたインスタンス変数の定義
sampleClass.h
@interface sampleClass:NSObject
{
   NSString *_string; //インスタンス変数の定義
   double _number; //インスタンス変数の定義
}
@property (nonatomic) NSString* string; //プロパティを記述すると、getterとsetterが自動で生成される
@end
sampleClass.m
@implementation sampleClass

@synthesize string = _string; //インスタンス変数とプロパティを明示的に紐付ける

//プロパティを記述していない場合、自分でgetterとsetterを作る
- (double)number{
   return _number;
}
- (void)setNumber:(double)number{
   _number = number;
}
@end
setter/getterを自分で作る場合の利点は、setter/getter内に好きなことを記述できる
例えば、_stringをセットしたついでに、UILabel.textに_stringをセットしたり

・self.propertyの使いどころ
getter/setter/init/deallocのみ_propertyを使い、
その他はself.propertyを使う

2014年10月6日月曜日

JASidePanelControllerでサイドパネルを開くたびに呼ばれるメソッド

JASidePanelControllerを使ってみて、サイドパネルを開くたびにビューを更新しようと思ったけど、デフォルトでその機能がないみたいなのでメモ

JASidePanelController.hに
- (void)prepareShowRightPanel;
を追記

JASidePanelController.mで
- (void)_showRightPanel:(BOOL)animated bounce:(BOOL)shouldBounce {
    self.state = JASidePanelRightVisible;
    [self _loadRightPanel];
    
    [self _adjustCenterFrame];
    
    if (animated) {
        [self _animateCenterPanel:shouldBounce completion:nil];
    } else {
        self.centerPanelContainer.frame = _centerPanelRestingFrame; 
        [self styleContainer:self.centerPanelContainer animate:NO duration:0.0f];
        if (self.style == JASidePanelMultipleActive || self.pushesSidePanels) {
            [self _layoutSideContainers:NO duration:0.0f];
        }
    }
    
    if (self.style == JASidePanelSingleActive) {
        self.tapView = [[UIView alloc] init];
    }
    [self prepareShowRightPanel];//これを追加
    [self _toggleScrollsToTopForCenter:NO left:NO right:YES];
}
次に、JASidePanelControllerのサブクラスに
- (void)prepareShowRightPanel {
    NSLog(@"show right panel");
}
を記述

これで、右のサイドパネルを開くたびに
show right panel
とログが表示される

2014年9月29日月曜日

2014年6月8日日曜日

【HTMLParser】HTMLのパース

HTMLParserを使うときのメモ

#import "HTMLParser.h"でインポートして、
プロジェクトのGeneralを選択

その中のLinked Frameworks and Librariesにて、
libxml2.dylibを追加する

次は、Build SettingsのSerchPathsのHeader Search Pathsに
$(SDKROOT)/usr/include/libxml2を追加する
(行の最後に半角スペースを入れて↑をコピペ)

完成です

2014年4月30日水曜日

【マルチスレッド】Objective Cのマルチスレッド実装方法

ObjCのマルチスレッドについてちょっと詳しくなったからメモ

1. GCDを使う
 前にも書いたけど、スレッドはGCDを使うのが楽
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
    [self doSomething]; //別スレッド
    dispatch_sync(dispatch_get_main_queue(), ^{
        [self doSomething]; // メインスレッド
    });
});
この場合、別スレッドに重い処理を書いて、メインスレッドはUIの描画が一般的らしい
PRIORITYで優先度も設定できる
そして、インスタンスメソッドはスレッドを指定して実行することができる
[self performSelectorOnMainThread:@selector(refresh)
                        withObject:nil
                    waitUntilDone:NO];
2. NSOperationを使う
NSOperationQueue *queue = [NSOperationQueue new];
    MyOperation *operation = [MyOperation new];//NSOperationを継承したクラスを作成
    [queue addOperation:operation];
 
3. NSInvocationOperationを使う
 この場合、NSOperationの継承クラスを作らなくてもいい
NSInvocationOperation *invOperation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(aaa) object:nil];
    [queue addOperation:invOperation];
4. とりあえずバックグラウンドに投げる
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{


        // ここに非同期でバックグラウンドに投げたい処理を書く

    });

2014年4月29日火曜日

【UICollectionView】カスタムセルをxibで作成

storyboard上でUICollectionViewを用意して、
xibで作ったUICollectionViewCellのカスタムクラスを配置する

1. file > new > file > UserInterface でviewを選択、ファイル名をmyCell.xib
 ラベルをUICollectionViewCellの上に配置する
2. 同じ名前の.hと.mファイルを作成する
3. myCell.xibのFIle's ownerのCustom ClassをmyCellに

 viewを選択して、identifierをmyCellに
 
  Custom CalssもmyCellに

myCellにプロパティを記述
myCell.h
 
#import 

@interface myCell : UICollectionViewCell

@property (strong, nonatomic) IBOutlet UILabel *cellLabel;


@end
myCell.m
 
#import "myCell.h"

@implementation myCell


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


-(void)awakeFromNib
{
    NSLog(@"bundleLoader waked");
}

@end


4. myCell.xibのFIle's ownerではなく、viewを選択して
 inspectorのoutletから用意したプロパティとxib上のラベルをつなぐ

このやりかたでないと、
 Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x6858780> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key… 

とエラーが...

5. あとはcollectionViewController.mで
 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    NSString *identifier = @"myCell";
    static BOOL nibCellLoaded = NO;
    
    if(!nibCellLoaded){
        UINib *nib = [UINib nibWithNibName:@"myCell" bundle:nil];
        [collectionView registerNib:nib forCellWithReuseIdentifier:identifier];
        nibCellLoaded = YES;
    }
    
    
    myCell *cell = (OBWeather*)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor greenColor];

    return cell;
}
 
 

最後に思ったけど、
クラス名とidentifierって一緒じゃない方がいいのかなあ

2014年4月25日金曜日

【iOS】メソッドのラベルを省略

Obj-Cで、引数が複数あるメソッドは通常

 - (リターン型)メソッド名:(引数型1)引数名1 ラベル:(引数型2)引数名2 ラベル:(引数型3)引数名3...

と続くが、ラベルは省略可能(推奨はされてない?)ということを知りました。
省略すると

  - (リターン型)メソッド名:(引数型1)引数名1:(引数型2)引数名2:(引数型3)引数名3...

 となります。呼び出す時は、

  [インスタンス名 メソッド名:引数1:引数2:引数3];

すごい違和感