![](/img/trans.png)
[英]Drawing text on UIView is better or using UILabel and updating text at run time?
[英]UIView frame not updating size on time for creating scrolling text animation in UILabel
我正在从控制器的cellForRowAtIndexPath内部执行通常的“将您自己的标签/图像添加到cell.contentview”。 我正在尝试使其中一个标签具有动画滚动文本:即,如果标签中的文本超出viewCell宽度,则单元格中的标签应滚动文本(动画-非手动)。 此外,整个tableview +滚动文本应同时适用于iPhone和iPad(因此,所有内容都需要相应地自动调整大小)。
追逐追赶,问题是:
如何获取cell.contentView的子视图以按时更新其帧大小,以测试此子视图中的文本是否溢出。
也许,这是否是正确的解决方法?
这是我的意思的图片(secondLabel等在下面说明):
现在您可以继续阅读以获取详细信息:
我尝试解决的方法是让cellForRowAtIndexPath将自定义UISCrollLabelView实例添加到单元格的contentView中。 这个自定义类实际上是一个内部管理UILabel的UIView。
UISCrollLabelView应该自动调整大小以使其不会溢出表格单元(适用于iPhone和iPad)。 然后,根据cellForRowAtIndexPath传递给它的文本的长度,它应该自动调整其内部标签的大小以适合所有文本,并且如果该标签最终导致视图(自身)溢出,请对UILabel的滚动进行动画处理。
我对此有几个问题,但主要的问题是:UIScrollableView基于比较其内部标签的frame.size.width与self.frame.size.width来触发(或不触发)动画。 但是很显然,self的帧宽度从0更新到插入cell.contenView后最终调整为它的大小需要花费一些时间。 这意味着,当我自定义UIScrollLabelView测试(label.frame.size.width > self.frame.size.width)
这始终是正确的,不会溢出的文本仍会滚动。 一秒钟左右,self.frame确实更新为正确的大小。
这是cellForRowAtIndexPath( secondLabel
是此处的scrollView):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIImageSmartView *cachedImage;
UILabel *mainLabel;
UIScrollLabelView *secondLabel;
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(70, 0, 0, 20)] autorelease];
mainLabel.font = [UIFont boldSystemFontOfSize:18];
mainLabel.textAlignment = UITextAlignmentLeft;
mainLabel.textColor = [UIColor blackColor];
mainLabel.backgroundColor = [UIColor clearColor];
mainLabel.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin);
mainLabel.tag = MAINLABEL_TAG;
secondLabel = [[[UIScrollLabelView alloc] initWithFrame:CGRectMake(70, 40, 0, 20)] autorelease];
secondLabel.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin);
//secondLabel.backgroundColor = [UIColor clearColor];
secondLabel.tag = SECONDLABEL_TAG;
cachedImage = [[UIImageSmartView alloc] initWithFrame:CGRectMake(5, 5, 57, 80)];
cachedImage.tag = PHOTO_TAG;
[cell.contentView addSubview:cachedImage];
[cell.contentView addSubview:mainLabel];
[cell.contentView addSubview:secondLabel];
}
else
{
cachedImage = (UIImageSmartView*)[cell.contentView viewWithTag:PHOTO_TAG];
mainLabel = (UILabel*)[cell.contentView viewWithTag:MAINLABEL_TAG];
secondLabel = (UIScrollLabelView*)[cell.contentView viewWithTag:SECONDLABEL_TAG];
}
// Configure the cell...
NSString *ImageName = [[dbData objectAtIndex:indexPath.row] objectAtIndex:2];
NSString *imageURL = [NSString stringWithFormat:@"http://someserver", referencingTable];
[cachedImage loadAndCacheImageFromFile:ImageName fromURL:imageURL inSize:CGSizeMake(57, 80) withBorderWidth:4];
mainLabel.text = [[dbData objectAtIndex:indexPath.row] objectAtIndex:0];
// -> At this point I load the text into the "scrolling label"
// (actually a UIView with a label)
[secondLabel setScrollingText:[[dbData objectAtIndex:indexPath.row] objectAtIndex:1]];
return cell;
}
到目前为止,我的UIScrollLabelView
实现看起来像这样:
@implementation UIScrollLabelView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// Initialization code
isScrolling = NO;
self.clipsToBounds = YES;
UILabel *label = [[[UILabel alloc] init] autorelease];
label.font = [UIFont systemFontOfSize:14.0];
label.textAlignment = UITextAlignmentLeft;
label.textColor = [UIColor darkGrayColor];
label.backgroundColor = [UIColor greenColor];
self.backgroundColor = [UIColor redColor];
//label.backgroundColor = [UIColor clearColor];
[self addSubview: label];
}
return self;
}
- (void)setScrollingText:(NSString*)text
{
[self setNeedsLayout];
UILabel *label = [[self subviews ] objectAtIndex:0];
label.text = text;
[label sizeToFit];
if(label.frame.size.width > self.frame.size.width)
[self scrollText];
}
- (void)scrollText
{
if(isScrolling)
return;
isScrolling = YES;
UILabel *label = [[self subviews ] objectAtIndex:0];
[UIView beginAnimations:@"scroll" context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDidStopSelector:@selector(scrollDidComplete)];
[UIView setAnimationDuration: label.frame.size.width/(float)100];
CGRect frame = label.frame;
if(frame.origin.x == 0)
frame.origin.x = frame.origin.x - (frame.size.width - self.frame.size.width);
else
frame.origin.x = 0;
label.frame = frame;
[UIView commitAnimations];
}
- (void)scrollDidComplete
{
isScrolling = NO;
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(scrollText) userInfo:nil repeats:NO];
}
@end
当尚未定义单元格的大小时,您将设置标签大小。 然后,您无需再检查实际大小。 您需要重写setFrame和setBounds来捕获帧大小更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.