LoginSignup
2
2

More than 5 years have passed since last update.

UILabelの背景に画像を設定するとXcode6でビルドしてiOS8で動かした場合だけ画像がズレる

Last updated at Posted at 2014-12-18

UIColor.colorWithPatternImageを使用することでUILabelの背景に画像を設定できるのですが、Xcode6でビルドしてiOS8で動かすと画像がおかしくなります。
原因はわかっていないのですがラベルに日本語を設定した場合だけズレるようです。
下記を追加することで対応できます。

label.clipsToBounds = YES;

コード

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    {
        UILabel *label = [[UILabel alloc] init];
        label.text = @"koreha daijoubu";
        label.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"test"]];
        label.frame = CGRectMake(100.0, 100.0, 124.0, 21.0);
        [self.view addSubview:label];
    }

    {
        UILabel *label = [[UILabel alloc] init];
        label.text = @"これはダメ";
        label.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"test"]];
        label.frame = CGRectMake(100.0, 100.0 + 100.0, 124.0, 21.0);
        [self.view addSubview:label];
    }

    {
        UILabel *label = [[UILabel alloc] init];
        label.text = @"こうするとOK";
        label.clipsToBounds = YES;
        label.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"test"]];
        label.frame = CGRectMake(100.0, 100.0 + 200.0, 124.0, 21.0);
        [self.view addSubview:label];
    }
}

iOS7
20141213002816.png

iOS8
20141213002826.png

2
2
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
2
2