NSAttributedString
エスケープされた改行文字 ( ) を使用して複数行を作成できます@"\n"
。UIImage
iOS 7 では、属性付き文字列の中に ( 経由で) を埋め込むことができるようになりましたNSTextAttachment
。
埋め込み画像を含む複数行の属性付き文字列にattributedText
aを設定すると、実際に表示される行数がラベルの高さに反比例することに気付きました。たとえば、ラベルの高さが 80 の場合、2 本の線が表示されます。高さが 100 前後の場合、2 行目のみが表示されます。高さが約 130 の場合、何も表示されません。UILabel
この問題は、複数の UILabel を a 内に並べて配置しようとしたときUITableViewCell
に、セルの高さに合わせてラベルが (垂直方向に) 大きくなるときに発生しました。
なぜこれが起こっているのか誰でも説明できますか?UILabel を小さくすることを伴わない回避策を知っている人はいますか?
サンプルコード:
@implementation SOViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableAttributedString *text1 = [[NSMutableAttributedString alloc] init];
[text1 appendAttributedString:[[NSAttributedString alloc] initWithString:@"Line 1\n"]];
[text1 appendAttributedString:[[NSAttributedString alloc] initWithString:@"Line 2"]];
UIImage *image = [UIImage imageNamed:@"17x10"]; //some PNG image (17px by 10px)
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = image;
attachment.bounds = CGRectMake(0, 0, image.size.width, image.size.height);
NSMutableAttributedString *text2 = [[NSMutableAttributedString alloc] init];
[text2 appendAttributedString:[[NSAttributedString alloc] initWithString:@"Line 1\n"]];
[text2 appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];
[text2 appendAttributedString:[[NSAttributedString alloc] initWithString:@"Line 2"]];
CGFloat margin = 20;
//shows both lines when height == 80
//shows line 2 when 90 <= height <= 120
//shows nothing when height == 130
CGFloat height = ???;
CGFloat width = 200;
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(margin, margin, width, height)];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(margin, margin + height, width, height)];
[self.view addSubview:label1];
[self.view addSubview:label2];
label1.backgroundColor = [UIColor orangeColor];
label2.backgroundColor = [UIColor blueColor];
label2.textColor = [UIColor whiteColor];
label1.numberOfLines = 0;
label2.numberOfLines = 0;
label1.attributedText = text1;
label2.attributedText = text2;
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(margin + width, margin + height, image.size.width, image.size.height);
[self.view addSubview:imageView];
}
@end
...これを「Single View Application」のデフォルトView Controllerに入れます。(お好きな画像をお選びいただけます。)