UILabel の横に画像を表示したいのですが、UILabel はテキストの長さが可変で、画像をどこに配置すればよいかわかりません。どうすればこれを達成できますか?
8 に答える
CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font
constrainedToSize:maximumLabelSize
lineBreakMode:yourLabel.lineBreakMode];
-[NSString sizeWithFont:forWidth:lineBreakMode:] は何に適していますか?
この質問にはあなたの答えがあるかもしれませんが、私にとってはうまくいきました。
2014 年には、以下の Norbert による非常に便利なコメントに基づいて、この新しいバージョンを編集しました。これはすべてを行います。乾杯
// yourLabel is your UILabel.
float widthIs =
[self.yourLabel.text
boundingRectWithSize:self.yourLabel.frame.size
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{ NSFontAttributeName:self.yourLabel.font }
context:nil]
.size.width;
NSLog(@"the width of yourLabel is %f", widthIs);
yourLabel.intrinsicContentSize.width
Objective-C / Swift用
迅速に
yourLabel.intrinsicContentSize().width
選択された回答は、iOS 6 以下で正しいものです。
iOS 7 では、sizeWithFont:constrainedToSize:lineBreakMode:
非推奨になりました。を使用することをお勧めしますboundingRectWithSize:options:attributes:context:
。
CGRect expectedLabelSize = [yourString boundingRectWithSize:sizeOfRect
options:<NSStringDrawingOptions>
attributes:@{
NSFontAttributeName: yourString.font
AnyOtherAttributes: valuesForAttributes
}
context:(NSStringDrawingContext *)];
戻り値は ではCGRect
ないことに注意してくださいCGSize
。iOS 7 でそれを使用している人々の助けになることを願っています。
iOS8 では sizeWithFont が廃止されました。 を参照してください。
CGSize yourLabelSize = [yourLabel.text sizeWithAttributes:@{NSFontAttributeName : [UIFont fontWithName:yourLabel.font size:yourLabel.fontSize]}];
必要なすべての属性を sizeWithAttributes に追加できます。設定できるその他の属性:
- NSForegroundColorAttributeName
- NSParagraphStyleAttributeName
- NSBackgroundColorAttributeName
- NSShadowAttributeName
等々。しかし、おそらく他のものは必要ないでしょう
アーロンのリンクを含む、他のSOの投稿にいくつかの原則を適用した後、私が思いついたものを次に示します。
AnnotationPin *myAnnotation = (AnnotationPin *)annotation;
self = [super initWithAnnotation:myAnnotation reuseIdentifier:reuseIdentifier];
self.backgroundColor = [UIColor greenColor];
self.frame = CGRectMake(0,0,30,30);
imageView = [[UIImageView alloc] initWithImage:myAnnotation.THEIMAGE];
imageView.frame = CGRectMake(3,3,20,20);
imageView.layer.masksToBounds = NO;
[self addSubview:imageView];
[imageView release];
CGSize titleSize = [myAnnotation.THETEXT sizeWithFont:[UIFont systemFontOfSize:12]];
CGRect newFrame = self.frame;
newFrame.size.height = titleSize.height + 12;
newFrame.size.width = titleSize.width + 32;
self.frame = newFrame;
self.layer.borderColor = [UIColor colorWithRed:0 green:.3 blue:0 alpha:1.0f].CGColor;
self.layer.borderWidth = 3.0;
UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(26,5,newFrame.size.width-32,newFrame.size.height-12)];
infoLabel.text = myAnnotation.title;
infoLabel.backgroundColor = [UIColor clearColor];
infoLabel.textColor = [UIColor blackColor];
infoLabel.textAlignment = UITextAlignmentCenter;
infoLabel.font = [UIFont systemFontOfSize:12];
[self addSubview:infoLabel];
[infoLabel release];
この例では、テキスト サイズに応じて UILabel のサイズを変更する MKAnnotation クラスにカスタム ピンを追加しています。また、ビューの左側に画像が追加されるため、画像とパディングを処理するための適切な間隔を管理するコードの一部が表示されます。
CGSize titleSize = [myAnnotation.THETEXT sizeWithFont:[UIFont systemFontOfSize:12]];
重要なのは、ビューの寸法を使用してから再定義することです。このロジックは、任意のビューに適用できます。
アーロンの答えは一部の人にとってはうまくいきますが、私にとってはうまくいきませんでした。これは、画像とサイズ変更可能な UILabel を備えたより動的なビューが必要な場合に、他の場所に行く前にすぐに試す必要がある、はるかに詳細な説明です。私はすでにあなたのためにすべての仕事をしました!!