gpt4 book ai didi

iOS - 具有约束的多行 UILabel 高度

转载 作者:行者123 更新时间:2023-11-29 12:40:27 27 4
gpt4 key购买 nike

我正在尝试创建一个可重用的消息 UIView 子类,它根据其 UILabel 中的文本调整其高度。这个答案说这是不可能的。真的是这样吗? iOS message cell width/height using auto layout

我的问题是 UILabel 的 CGFrame 的高度太大。 (UILabel 具有绿色背景色。)

The height is much too large for the next needed.

这是我的代码(顺便说一下,[autolayoutView]translatesAutoresizingMaskIntoConstraints 设置为 NO):

SSLStickyView *stickyView = [[SSLStickyView alloc] initWithText:@"Try keeping a steady beat to help me get past the notes! Press the bass drum to jump!"];

SSLStickyView.m

- (instancetype)initWithText:(NSString *)text
{
self = [super initWithFrame:CGRectZero];
if (self)
{

_stickyImageView = [UIImageView autoLayoutView];
_stickyImageView.backgroundColor = [UIColor blueColor];
_stickyImageView.image = [UIImage imageNamed:@"element_sticky"];
[self addSubview:_stickyImageView];

float padding = 5;
NSMutableAttributedString *attributedText =
[[NSMutableAttributedString alloc]
initWithString:text
attributes:@
{
NSFontAttributeName: [UIFont boldSystemFontOfSize:30],
NSForegroundColorAttributeName: [UIColor purpleColor]
}];

UILabel *textLabel = [UILabel autoLayoutView];
textLabel.preferredMaxLayoutWidth = 50;
textLabel.attributedText = attributedText;
textLabel.numberOfLines = 0; // unlimited number of lines
textLabel.lineBreakMode = NSLineBreakByWordWrapping;
textLabel.backgroundColor = [UIColor greenColor];

[_stickyImageView addSubview:textLabel];

NSLayoutConstraint *stickyWidthPin =
[NSLayoutConstraint constraintWithItem:_stickyImageView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:textLabel
attribute:NSLayoutAttributeWidth
multiplier:1
constant:padding * 2];
NSLayoutConstraint *stickyHeightPin =
[NSLayoutConstraint constraintWithItem:_stickyImageView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:textLabel
attribute:NSLayoutAttributeHeight
multiplier:1
constant:0];
NSLayoutConstraint *stickyTextLabelTop =
[NSLayoutConstraint constraintWithItem:_stickyImageView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:textLabel
attribute:NSLayoutAttributeTop
multiplier:1
constant:0];
NSLayoutConstraint *stickyTextLeftPin = [NSLayoutConstraint constraintWithItem:_stickyImageView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:textLabel
attribute:NSLayoutAttributeLeft
multiplier:1
constant:-padding * 2];
NSDictionary *views = NSDictionaryOfVariableBindings(_stickyImageView, textLabel);
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[_stickyImageView]" options:0 metrics:nil views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_stickyImageView]" options:0 metrics:nil views:views]];
[self addConstraints:@[stickyWidthPin, stickyHeightPin, stickyTextLeftPin, stickyTextLabelTop]];
self.backgroundColor = [UIColor whiteColor];

}

return self;
}

最佳答案

让您的 super View 了解其内容大小的变化,并相应地调整 super View 的宽度和高度。由于如何执行此操作可能不是很明显,我提供了一个 UIView 子类,它将根据其内容(一个 UILabel)调整自身大小。

注意:仅向 ReusableMessageView 添加会影响其在其父 View 中的位置的约束。 ReusableMessageView 将根据消息调整其宽度/高度。

@interface ReusableMessageView : UIView
-(instancetype)initWithMessage:(NSString *)message preferredWidth:(CGFloat)width;
-(void)setMessage:(NSString *)message;
@end

@implementation ReusableMessageView {
UILabel *_label;
}

-(instancetype)initWithMessage:(NSString *)message preferredWidth:(CGFloat)width {
if (self = [super init]) {
self.translatesAutoresizingMaskIntoConstraints = NO;
//setup label
_label = [UILabel new];
_label.translatesAutoresizingMaskIntoConstraints = NO;
_label.text = message;
_label.preferredMaxLayoutWidth = width;
_label.numberOfLines = 0;
[self addSubview:_label];
}
return self;
}

-(void)layoutSubviews {
[super layoutSubviews];
// remove all previously added constraints
[self removeConstraints:self.constraints];

CGFloat width = _label.bounds.size.width;
CGFloat height = _label.bounds.size.height;

NSLayoutConstraint *c1,*c2,*c3,*c4;
// set the view's width/height to be equal to the label's width/height
c1 = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:width];
c2 = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:height];
// center the label
c3 = [NSLayoutConstraint constraintWithItem:_label attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];
c4 = [NSLayoutConstraint constraintWithItem:_label attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0];
// add all constraints
[self addConstraints:@[c1,c2,c3,c4]];
}

-(void)setMessage:(NSString *)message {
_label.text = message;
// once the message changes, the constraints need to be adjusted
[self setNeedsLayout];
}
@end

这可以通过重用现有约束并仅更改每个约束的“常量”属性来改进。如果这样做,您甚至可以为变化设置动画。

这是一个在 viewController 的 viewDidLoad 方法中使用的例子:

ReusableMessageView *view = [[ReusableMessageView alloc]initWithMessage:@"This is the first message that is rather long in order to exaggerate the change in size" preferredWidth:50.0];
[self.view addSubview:view];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
// demonstrate the change in size
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[view setMessage:@"This is the second message"];
});

关于iOS - 具有约束的多行 UILabel 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25099981/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com