Scrolling UILabel like a marquee in a subview

后端 未结 4 707
-上瘾入骨i
-上瘾入骨i 2020-12-17 05:47

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.

Basical

4条回答
  •  不思量自难忘°
    2020-12-17 06:37

    Apparently the culprit was AutoLayout.

    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!!

    EDIT: Solution with 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];
    

提交回复
热议问题