LoginSignup
12
9

More than 5 years have passed since last update.

UILabelに画像を設定する

Posted at

UILabelに画像を設定するにはUILabelのattributedTextプロパティに
NSTextAttachmentを設定したNSAttributedStringを設定します。

UILabelに画像を設定する
    NSMutableAttributedString *as = [[NSMutableAttributedString alloc] initWithString:@"message"];
    NSTextAttachment *at = [[NSTextAttachment alloc] init];
    at.image = [UIImage imageNamed:@"sample"];
    at.bounds = CGRectMake(0,
                           1,
                           at.image.size.width/[[UIScreen mainScreen] scale],
                           at.image.size.height/[[UIScreen mainScreen] scale]);

    NSAttributedString *ns = [NSAttributedString attributedStringWithAttachment:at];
    [as insertAttributedString:ns atIndex:0];//画像をinsertするインデックスを指定

    self.myLabel.attributedText = as;
12
9
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
12
9