LoginSignup
15
13

More than 5 years have passed since last update.

StoryboardでLabelのAttributeを設定しつつ、文字をコードで動的に変える

Posted at

LabelのAttributeはStoryboard見ながら設定したい、でも文字はコードで動的に変えたい!
でも、UILabelのattributedTextはimmutableなのでコード側で文字を変えるのは面倒です。

そんな時はLabelのattributesを取ってきて、それを使って新しくattributedStringを生成するとうまくいきます。

NSString *text = @"....";
NSDictionary *attributes = [self.label.attributedText attributesAtIndex:0 effectiveRange:nil];
self.label.attributedText = [[NSAttributedString alloc] initWithString:text attributes:attributes];

15
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
15
13