我正在从 xib 加载自定义UIView
。多行时UILabel
文本可能很长。 UIButton
下方UILabel
。 UIView
正在从xib 加载为自定义子类。
我需要调整整个UIView
的大小,具体取决于文本的数量。但它保持在 xib 中设置的框架高度。如何使用UIView
文本弹性调整UILabel
的大小,以及保持所有autolayout
约束不变。
我需要UIView
框架尺寸,因为我将它显示为带有动画的顶部的下拉视图。
UILabel
的大小正常。但是文字超出了UIView
。 UIView
需要调整大小,我需要更新的帧大小。
这些行修复了 autoresize ,但我不确定这是正确的方法。
CGRect myFrame = self.frame;
self.frame = CGRectMake(myFrame.origin.x, myFrame.origin.y,
myFrame.size.width,
self.labelMessage.frame.size.height + self.btClose.frame.size.height + 16); // magic number!
全班,
#import "MessageView.h"
@implementation MessageView
@synthesize systemMessage = _systemMessage; //otherwise setter is not called
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
[self updateUI];
}
- (void) updateUI {
// DLog(@"%@", _systemMessage);
if (_systemMessage.actionType == MessageActionDisappearing || _systemMessage.actionType == MessageActionDismissableRetry) {
_btTryAgain.hidden = NO;
} else {
_btTryAgain.hidden = YES;
}
if (_systemMessage.actionType == MessageActionDismissable || _systemMessage.actionType == MessageActionDismissableRetry) {
_btClose.hidden = NO;
} else {
_btClose.hidden = YES;
}
_borderView.backgroundColor = [self getBorderColor];
_labelMessage.text = _systemMessage.message;
CGRect myFrame = self.frame;
self.frame = CGRectMake(myFrame.origin.x, myFrame.origin.y, myFrame.size.width,
self.labelMessage.frame.size.height + self.btClose.frame.size.height + 16); // magic number!
}
- (instancetype)initWithFrame:(CGRect)frame {
// DLog(@"");
self = [super initWithFrame:frame];
[self xibSetup];
return self;
}
- (void)awakeFromNib {
// DLog(@"");
[super awakeFromNib];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
// DLog(@"");
self = [super initWithCoder:aDecoder];
if (self.subviews.count == 0) {
[self xibSetup];
}
return self;
}
- (void) xibSetup {
UIView *v = [self loadViewFromXib];
v.frame = self.bounds;
[self addSubview:v];
}
- (UIView *)loadViewFromXib {
UIView *v = [[[NSBundle mainBundle] loadNibNamed:@"MessageView" owner:self options:nil] firstObject];
return v;
}
// isn't called!
- (CGSize)intrinsicContentSize
{
DLog(@"%@", _labelMessage.text);
CGSize size = self.labelMessage.intrinsicContentSize;
size.height = size.height + _btTryAgain.frame.size.height + 16;
size.width = self.intrinsicContentSize.width;
// size.width += 16;
return size;
}
#pragma mark - Methods
/** Refresh the UI when message content is changed. */
- (void)setSystemMessage:(SystemMessage *)systemMessage {
_systemMessage = systemMessage;
[self updateUI];
}
- (UIColor *)getBorderColor {
if (_systemMessage.messageType == MessageTypeWarning) {
return [UIColor yellowColor];
} else if (_systemMessage.messageType == MessageTypePositive) {
return [UIColor greenColor];
} else {
return [UIColor blueColor];
}
}
- (void) hideButtonWithId:(MessageViewButtonId)buttonId {
if (buttonId == MessageViewButtonIdClose) {
_btClose.hidden = YES;
} else if (buttonId == MessageViewButtonIdTryAgain) {
_btTryAgain. hidden = YES;
}
}
- (NSString *)description {
return [NSString stringWithFormat:@"%@, systemMessage: %@", [super description], _systemMessage];
}
#pragma mark - Actions
- (IBAction)didPressCloseButton:(id)sender {
if ([_delegate respondsToSelector:@selector(messageView:didPressButtonWithId:)]) {
[_delegate messageView:self didPressButtonWithId:MessageViewButtonIdClose];
}
}
- (IBAction)didPressTryAgainButton:(id)sender {
if ([_delegate respondsToSelector:@selector(messageView:didPressButtonWithId:)]) {
[_delegate messageView:self didPressButtonWithId:MessageViewButtonIdTryAgain];
}
}