How can i apply a horizontal marquee effect in UITextView in objective c

时间:2017-06-15 09:37:05

标签: ios objective-c

I want to add a marquee effect in an non editable UITextView with maximum lines 1..I dont want to use UILabel due to some limitations...I did found Marquee for UILabel but not found anything for UITextView..Is there any library or some idea of how to do that..

1 个答案:

答案 0 :(得分:0)

Edit: Modified the answer to handle dynamic string length by calculating the string width.

There are some possibilities that could work - one of which is as follow. The idea is to put UITextView inside a UIView and enable clipToBounds property of that UIView. Keeping the height of UITextView a constant while setting its width to a large amount. Which would be width of its parent UIView + 2 x width of text. Keep the alignment of UITextView to left and keeping UITextView itself center aligned in the UIView. After setting that all up, we can do scrolling of UITextView via code :

- (void)setTextViewWidth
{
    NSString * loremIpsa = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.";

    textView.text = loremIpsa;

    UIFont *font = textView.font;
    textWidth = [self widthOfString:loremIpsa withFont:font];

    CGRect frame = textView.frame;
    frame.size.width = parentView.frame.size.width + textWidth * 2;
    frame.origin.x = -1 * textWidth;
    textView.frame = frame;
}

- (void)startMarqueeing
{
    [NSTimer scheduledTimerWithTimeInterval:(0.01)
                                     target:self selector:@selector(autoscrollTimerFired) userInfo:nil repeats:YES];
    NSLog(@"-----------  %f %f",textView.contentSize.width , textView.contentSize.height);
}

- (void) autoscrollTimerFired
{
    CGPoint scrollPoint = textView.contentOffset;
    NSLog(@"+++++++++++ %.2f %.2f",scrollPoint.x,scrollPoint.y);
    scrollPoint = CGPointMake(scrollPoint.x+1, scrollPoint.y);

    if(scrollPoint.x >= textView.contentSize.width - textWidth)
    {
        scrollPoint.x = 0;
    }

    [textView setContentOffset:scrollPoint animated:NO];


}

- (CGFloat)widthOfString:(NSString *)string withFont:(UIFont *)font {
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
    return [[[NSAttributedString alloc] initWithString:string attributes:attributes] size].width;
}

This should work, I tested the code with both small and large strings .Let me know if you need further assistance on the matter. Note that textWidth is a class variable declared in .h file.