LoginSignup
13
13

More than 5 years have passed since last update.

UILabelの上下左右に任意に枠線をつけるには

Last updated at Posted at 2015-02-06

はじめに

例えばUILabelの右端のみ、下辺のみ枠線をつけようとした場合、標準の機能ではなかなか手軽にできなかったりします。

色々な解決法があると思いますが、実運用でも使用している一例として以下の方法を共有します。

UILabelに限らず適用できます。

解決方法「UIViewを4辺に置く」

任意のUIViewの4辺にUIViewを付加することで実現しています。
シンプルに。

付加用メソッド作成

ユーティリティクラスにて定義
+ (void)assignBorders:(UIView *)view borderTop:(BOOL)borderTop borderBottom:(BOOL)borderBottom borderLeft:(BOOL)borderLeft borderRight:(BOOL)borderRight
{
    UIView *border = nil;
    UIColor *color = [UIColor darkGrayColor];

    if (borderTop) {
        border = [[UIView alloc] initWithFrame:CGRectMake(0, 0, view.bounds.size.width, 1)];
        [border setBackgroundColor:color];
        [view addSubview:border];
        [border release];
    }

    if (borderBottom) {
        border = [[UIView alloc] initWithFrame:CGRectMake(0, view.bounds.size.height - 1, view.bounds.size.width, 1)];
        [border setBackgroundColor:color];
        [view addSubview:border];
        [border release];
    }

    if (borderLeft) {
        border = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, view.bounds.size.height)];
        [border setBackgroundColor:color];
        [view addSubview:border];
        [border release];
    }

    if (borderRight) {
        border = [[UIView alloc] initWithFrame:CGRectMake(view.bounds.size.width - 1, 0, 1, view.bounds.size.height)];
        [border setBackgroundColor:color];
        [view addSubview:border];
        [border release];
    }
}

使用する

UILabelに対して使用
    [XXX assignBorders:[ラベル] borderTop:NO borderBottom:NO borderLeft:NO borderRight:YES];
UIImageViewを生成し使用した場合
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 105, 105)];
    [imageView setImage:[UIImage imageNamed:@"noImage"]];
    [XXX assignBorders:imageView borderTop:YES borderBottom:YES borderLeft:NO borderRight:YES];

コードでコントロール生成時に使用するパターン

コードでコントロールを生成する
こちらと連動しコントロール生成時に枠線を指定するパターン。

+ (UILabel *)newLabel:(CGRect)rect text:(NSString *)text alignment:(int)alignment borderTop:(BOOL)borderTop borderBottom:(BOOL)borderBottom borderLeft:(BOOL)borderLeft borderRight:(BOOL)borderRight
{
    UILabel *label = [[UILabel alloc] init];

    [label setFrame:rect];
    [label setText:text];
    [label setFont:[UIFont systemFontOfSize:16]];
    [label setTextColor:[UIColor blackColor]];
    [label setTextAlignment:alignment];
    [label setNumberOfLines:0];
    [label setLineBreakMode:NSLineBreakByWordWrapping];
    [label setBackgroundColor:[UIColor clearColor]];

    [self assignBorders:label borderTop:borderTop borderBottom:borderBottom borderLeft:borderLeft borderRight:borderRight];

    return label;
}
ラベル生成メソッドと同時使用
    UILabel *lbl = [XXX newLabel:CGRectMake(100, 100, 120, 30) text:@"サンプル" alignment:NSTextAlignmentCenter borderTop:YES borderBottom:YES borderLeft:YES borderRight:YES];
    [self addSubview:lbl];

おわりに

同じ課題で悩んだ人の参考になりましたら幸いです!

13
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
13