在子视图中滚动UILabel像一个选框 [英] Scrolling UILabel like a marquee in a subview

查看:104
本文介绍了在子视图中滚动UILabel像一个选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在主视图中有一个UILabel文本 - 非常长的文本。正确的宽度是142,但我把它缩短为55.

I have a UILabel in the main view with text - "Very Very long text". The proper width to this would be 142, but i've shortened it to 55.

基本上我想实现一个滚动类型滚动,所以我编写代码添加它。

Basically I want to implement a marquee type scroll, so I wrote code to add it onto a subview and animate it within the bounds of that view.

CODE -

    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];

之后,我在模拟器上得到这个输出 - >

After this I get this output on the simulator -->

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

The problem occurs when i try the Animation with this code -

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

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

I was hoping I would see the entire "Very Very long text", rather only "Very..." scrolls from left to right.

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

To solve this I added one line of code --

    //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内部设置全文,它会正确滚动。但是在模拟器中运行会给我这个 -

I thought the full text will be set inside the newly added UIView and it would scroll properly. But running in the simulator gave me this --

再次,只有Very ...从左到右滚动。

And again, only "Very..." was scrolling from left to right.

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

What am I doing wrong? Please help!!



EDIT



EDIT

Apparently the culprit was AutoLayout.

我不知道为什么,但一旦我取消选中使用Autolayout视图
XIB,一切都开始按预期工作。设置
tempLblFrame.origin.x = -_lblLongText.intrinsicContentSize.width;

I have no clue why, but once I unchecked "Use Autolayout" for the view in the XIB, everything started working as expected. Setting tempLblFrame.origin.x = -_lblLongText.intrinsicContentSize.width; was working properly and so was the scroll.

对此有任何解释吗?


推荐答案

显然,罪魁祸首是AutoLayout。

Apparently the culprit was AutoLayout.

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

I have no clue why, but once I unchecked "Use Autolayout" for the view in the XIB, everything started working as expected. Setting tempLblFrame.origin.x = -_lblLongText.intrinsicContentSize.width; was working properly and so was the scroll.

仍然,更好的解释肯定会有帮助!!

Still, a better explanation for this would surely help!!

    //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];

这篇关于在子视图中滚动UILabel像一个选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆