滚动UILabel就像子视图中的选取框一样

时间:2013-06-25 12:05:03

标签: ios objective-c cocoa-touch uilabel

我在主视图中有一个带文字的UILabel - “非常长的文字”。适当宽度为142,但我已将其缩短为55.

基本上我想实现一个字幕类型滚动,所以我编写代码将它添加到子视图中并在该视图的边界内设置动画。

代码 -

    CGRect tempLblFrame = _lblLongText.frame;
    UIView *lblView = [[UIView alloc] initWithFrame:tempLblFrame];

    //Add label to UIView at 0,0 wrt to new UIView
    tempLblFrame.origin.x = 0;
    tempLblFrame.origin.y = 0;

    [_lblLongText setFrame:tempLblFrame];
    [_lblLongText removeFromSuperview];
    [lblView addSubview:_lblLongText];

    //SetClipToBounds so that if label moves out of bounds of its superview, it wont be displayed
    [lblView setClipsToBounds:YES];
    [lblView setBackgroundColor:[UIColor cyanColor]];
    [self.view addSubview:lblView];

在此之后我在模拟器上得到这个输出 - > enter image description here

当我使用此代码尝试动画时会出现问题 -

    tempLblFrame.origin.x = -_lblLongText.intrinsicContentSize.width;        
    [UIView animateWithDuration:2.0 delay:1.0 options:UIViewAnimationOptionCurveLinear
                     animations:^{
                         [_lblLongText setFrame:tempLblFrame];
                     }
                     completion:^(BOOL finished) {
                         NSLog(@"completed");
                     }];

我希望我能看到整个“非常长的文字”,而只是“非常......”从左到右滚动。

要解决此问题,我添加了一行代码 -

    //Add label to UIView at 0,0 wrt to new UIView
    tempLblFrame.origin.x = 0;
    tempLblFrame.origin.y = 0;

    tempLblFrame.size.width = _lblLongText.intrinsicContentSize.width; //THIS LINE WAS ADDED

    [_lblLongText setFrame:tempLblFrame];
    [_lblLongText removeFromSuperview];
    [lblView addSubview:_lblLongText];

我认为全文将在新添加的UIView中设置,并且它会正确滚动。但是在模拟器中运行给了我这个 -

enter image description here

而且,只有“非常......”从左向右滚动。

我做错了什么?请帮忙!!

  
    

修改

         

显然罪魁祸首是AutoLayout。

  
     

我不知道为什么,但是一旦我取消选中“使用Autolayout”视图   在XIB中,一切都按预期开始工作。设置   tempLblFrame.origin.x = -_lblLongText.intrinsicContentSize.width;是   工作正常,卷轴也是如此。

     

对此有任何解释!!?

4 个答案:

答案 0 :(得分:5)

这个问题可能是Duplicate of

虽然查尔斯鲍威尔为MarqueeLabel编写了很好的代码片段,

另请参阅This link

我希望这会对您有所帮助,并通过提供所需的输出来节省您的时间。

答案 1 :(得分:0)

使UILabel文本的宽度(或更长)和UIView想要查看的滚动区域。然后将UIView的{​​{1}}设置为clipToBounds(您正在执行此操作)。然后,当您从左到右进行动画处理时,您只能看到YES的宽度,因为它会剪切任何额外的子视图。只需确保滚动UIView

的整个长度

现在您将视图和标签的高度和宽度设置为相同的内容。这就是为什么你要剪切文本,而不是剪切标签。

答案 2 :(得分:0)

显然罪魁祸首是AutoLayout。

我不知道为什么,但是一旦我取消选中“使用Autolayout”作为XIB中的视图,一切都按预期开始工作。设置tempLblFrame.origin.x = -_lblLongText.intrinsicContentSize.width;工作正常,滚动也是如此。

但是,对此更好的解释肯定会有所帮助!!

编辑:AutoLayout解决方案 -

    //Make UIView for Label to sit in
    CGRect tempLblFrame = _lblLongText.frame;
    UIView *lblView = [[UIView alloc] initWithFrame:tempLblFrame];

    //#CHANGE 1 Removing all constraints 
    [_lblLongText removeConstraints:_lblLongText.constraints]; 

    //Add label to UIView at 0,0 wrt to new UIView
    tempLblFrame.origin.x = 0;
    tempLblFrame.origin.y = 0;
    //Set Full length of Label so that complete text shows (else only truncated text will scroll)
    tempLblFrame.size.width = _lblLongText.intrinsicContentSize.width;

    //#CHANGE 2 setting fresh constraints using the frame which was manually set 
    [_lblLongText setTranslatesAutoresizingMaskIntoConstraints :YES];
    [_lblLongText setFrame:tempLblFrame];
    [_lblLongText removeFromSuperview];
    [lblView addSubview:_lblLongText];

答案 3 :(得分:0)

您可以在视图滚动视图中添加并在滚动视图中添加此标签。使用此代码

 scroll.contentSize =CGSizeMake(100 *[clubArray count],20);
NSString  *bname;
bname=@"";
for(int i = 0; i < [clubArray count];  i++)
{
    bname = [NSString stringWithFormat:@"%@  %@ ,",bname,[[clubArray objectAtIndex:i] objectForKey:@"bottle_name"]];
    [bname retain];

}
UILabel *lbl1 = [[UILabel alloc] init];
[lbl1 setFrame:CGRectMake(0,5,[clubArray count]*100,20)];
lbl1.backgroundColor=[UIColor clearColor];
lbl1.textColor=[UIColor whiteColor];
lbl1.userInteractionEnabled=YES;    
[scroll addSubview:lbl1];
lbl1.text= bname;

这是实现的代码。谢谢