这个使用uilabel自调节高度发现的问题,代码如下:
//content
label
NSString *contentValue = ((MessageDetailRecord *)[self.entriesArray
objectAtIndex:indexPath.row]).content;
CGFloat width =
0.0f;
CGSize
orignalSize;
#pragma clang diagnostic push
#pragma clang diagnostic ignored
"-Wdeprecated-declarations"
if ([GeneralInfo
getDeviceSystemVer]){
orignalSize = [contentValue sizeWithAttributes:@{NSFontAttributeName:[UIFont
systemFontOfSize:20.0f]}];
width =
orignalSize.width;
}else{
width = [contentValue sizeWithFont:[UIFont systemFontOfSize:20.0f
]].width;
}
#pragma clang diagnostic
pop
//set the most large
width
if
(width > cell.frame.size.width - 90.0f)
{
width = cell.frame.size.width -
90.0f;
}
UILabel *content = [[UILabel alloc]initWithFrame:CGRectMake(0.0f, 0.0f,
width,
0.0f)];
self.messageContentLabel =
content;
self.messageContentLabel.font = [UIFont
systemFontOfSize:20.0];
self.messageContentLabel.backgroundColor = [UIColor
clearColor];
self.messageContentLabel.numberOfLines =
0;
self.messageContentLabel.adjustsFontSizeToFitWidth =
YES;
self.messageContentLabel.lineBreakMode =
NSLineBreakByWordWrapping;
self.messageContentLabel.backgroundColor = [UIColor colorWithRed:241/255.0f
green:241/255.0f blue:241/255.0f
alpha:1.0];
CGRect labelFrameContent = self.messageContentLabel.frame;
#pragma clang
diagnostic push
#pragma clang diagnostic ignored
"-Wdeprecated-declarations"
if
([GeneralInfo getDeviceSystemVer])
{
NSDictionary *stringAttributes = [NSDictionary
dictionaryWithObject:self.messageContentLabel.font forKey:
NSFontAttributeName];
labelFrameContent.size = [contentValue boundingRectWithSize:CGSizeMake(width,
CGFLOAT_MAX)
options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin
attributes:stringAttributes
context:nil].size;
}else{
labelFrameContent.size = [contentValue
sizeWithFont:self.messageContentLabel.font
constrainedToSize:CGSizeMake(width,
CGFLOAT_MAX)
lineBreakMode:self.messageContentLabel.lineBreakMode];
}
#pragma clang diagnostic
pop
//adjust the
height
if
(labelFrameContent.size.height < 50.0f)
{
labelFrameContent.size.height =
50.0f;
}
labelFrameContent.size.width +=
10.0f;
if
(labelFrameContent.size.width > cell.frame.size.width - 90.0f)
{
labelFrameContent.size.width = cell.frame.size.width -
90.0f;
}
labelFrameContent.origin.x =
70.0f;
labelFrameContent.origin.y = self.cellDateLabel.frame.size.height +
10.0f;
self.messageContentLabel.frame =
labelFrameContent;
[cell.contentView addSubview:self.messageContentLabel];
解决方法是使用uitextview来替换即可
UITextView *content = [[UITextView alloc]initWithFrame:CGRectMake(0.0f, 0.0f,
width,
0.0f)];
self.messageContentLabel =
content;
[content
release];
self.messageContentLabel.font = [UIFont
systemFontOfSize:16.0f];
self.messageContentLabel.backgroundColor = [UIColor
clearColor];
self.messageContentLabel.scrollEnabled =
NO;
self.messageContentLabel.userInteractionEnabled =
NO;
//self.messageContentLabel.numberOfLines =
0;
//self.messageContentLabel.adjustsFontSizeToFitWidth =
YES;
// self.messageContentLabel.lineBreakMode =
NSLineBreakByWordWrapping;
self.messageContentLabel.backgroundColor = [UIColor colorWithRed:241/255.0f
green:241/255.0f blue:241/255.0f
alpha:1.0];
self.messageContentLabel.text =
contentValue;
self.messageContentLabel.textColor = [UIColor
grayColor];
CGRect labelFrameContent =
self.messageContentLabel.frame;
#pragma clang diagnostic ignored
"-Wdeprecated-declarations"
if ([GeneralInfo
getDeviceSystemVer])
{
labelFrameContent.size = [contentValue boundingRectWithSize:CGSizeMake(width,
CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading)
attributes:@{NSFontAttributeName:[UIFont
systemFontOfSize:17.0f]}
context:nil].size;
}
else
{
labelFrameContent.size = [contentValue
sizeWithFont:self.messageContentLabel.font constrainedToSize:CGSizeMake(width,
CGFLOAT_MAX)];
}
#pragma clang diagnostic pop