如何滚动标签上的文本(如选框)

发布于 2024-10-19 16:23:01 字数 70 浏览 2 评论 0原文

我的标签上的文本没有完全显示,所以我希望文本在标签上从左到右滚动。请不要建议将 numberoflines 属性作为解决方案。

I have text on label which is not appearing completely so i want text to scroll on label from left to right. Please don't suggest numberoflines property as a solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁〃客ぐ 2024-10-26 16:23:01

FWIW,我在滚动视图中使用 2 个标签做了类似的事情......

#import <UIKit/UIKit.h>

@interface ScrolledTextView : UIScrollView
{
    UILabel *label1;
    UILabel *label2;

    NSString *_text;
    NSString *newText;

    CGFloat textWidth;

    BOOL animating;
    BOOL needToUpdateText; 
}

@property (nonatomic, copy, setter = setText:) NSString *text;
@property (nonatomic, assign) NSTimeInterval   scrollInterval;
@property (nonatomic, assign) NSTimeInterval   scrollDuration;

- (id) initWithFrame:(CGRect)frame andText : (NSString*) text;

- (void) setText:(NSString *)text;

- (BOOL) textFitsInFrame;

@implementation ScrolledTextView

@synthesize text = _text;
@synthesize scrollDuration;
@synthesize scrollInterval;

- (void) adjustLabelsForNewText : (NSString*) text andParentFrame : (CGRect) frame
{
    _text = [NSString stringWithFormat:@"%@  ", text];

    CGSize textSize = [[self text] sizeWithFont:[label1 font]     constrainedToSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) lineBreakMode:UILineBreakModeClip];
    [self setContentSize:CGSizeMake(textSize.width * 2, textSize.height)];

    textWidth = textSize.width;

    [label1 setFrame:CGRectMake(0.0, 0.0, textWidth, frame.size.height)];
    [label1 setText:[self text]];

    if([self textFitsInFrame])
    {
        [label2 removeFromSuperview];
    }
    else 
    {
        [label2 setFrame:CGRectMake([label1 frame].size.width, 0.0, textWidth, frame.size.height)];
        [label2 setText:[self text]];

        if( ! [[self subviews] containsObject:label2])
        {
            [self addSubview:label2];
        }

        [self beginScrolling];
    }
}

- (id) initWithFrame:(CGRect)frame andText : (NSString*) text
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setClipsToBounds:YES];

        animating = NO;
        needToUpdateText = NO;

        [self setScrollDuration:10.0];
        [self setScrollInterval:2.0];

        label1 = [UILabel new];
        label2 = [UILabel new];

        [label1 setBackgroundColor:[UIColor clearColor]];
        [label2 setBackgroundColor:[UIColor clearColor]];

        [self addSubview:label1];

        [self adjustLabelsForNewText:text andParentFrame:frame];        
    }

    return self;
}

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    if(needToUpdateText)
    {
        animating = NO;
        needToUpdateText = NO;
        [self adjustLabelsForNewText:@"" andParentFrame:[self frame]];
        [self adjustLabelsForNewText:newText andParentFrame:[self frame]];
        newText = nil;

        return;
    }

    [self setContentOffset:CGPointMake(0.0, 0.0)];
    [self beginScrolling];
}

- (void) beginScrolling
{
    animating = YES;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:[self scrollDuration]];
    [UIView setAnimationDelay:[self scrollInterval]];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];

    [self setContentOffset:CGPointMake(textWidth, 0.0)];

    [UIView commitAnimations];
}

- (BOOL) textFitsInFrame
{
    return textWidth <= [self frame].size.width;
}

- (void) setText:(NSString *)text
{
    if(animating)
    {
        newText = text;
        needToUpdateText = YES;
    }
    else 
    {
        [self adjustLabelsForNewText:text andParentFrame:[self frame]];
    }
}

FWIW, I did something similar using 2 labels in a scroll view...

#import <UIKit/UIKit.h>

@interface ScrolledTextView : UIScrollView
{
    UILabel *label1;
    UILabel *label2;

    NSString *_text;
    NSString *newText;

    CGFloat textWidth;

    BOOL animating;
    BOOL needToUpdateText; 
}

@property (nonatomic, copy, setter = setText:) NSString *text;
@property (nonatomic, assign) NSTimeInterval   scrollInterval;
@property (nonatomic, assign) NSTimeInterval   scrollDuration;

- (id) initWithFrame:(CGRect)frame andText : (NSString*) text;

- (void) setText:(NSString *)text;

- (BOOL) textFitsInFrame;

@implementation ScrolledTextView

@synthesize text = _text;
@synthesize scrollDuration;
@synthesize scrollInterval;

- (void) adjustLabelsForNewText : (NSString*) text andParentFrame : (CGRect) frame
{
    _text = [NSString stringWithFormat:@"%@  ", text];

    CGSize textSize = [[self text] sizeWithFont:[label1 font]     constrainedToSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) lineBreakMode:UILineBreakModeClip];
    [self setContentSize:CGSizeMake(textSize.width * 2, textSize.height)];

    textWidth = textSize.width;

    [label1 setFrame:CGRectMake(0.0, 0.0, textWidth, frame.size.height)];
    [label1 setText:[self text]];

    if([self textFitsInFrame])
    {
        [label2 removeFromSuperview];
    }
    else 
    {
        [label2 setFrame:CGRectMake([label1 frame].size.width, 0.0, textWidth, frame.size.height)];
        [label2 setText:[self text]];

        if( ! [[self subviews] containsObject:label2])
        {
            [self addSubview:label2];
        }

        [self beginScrolling];
    }
}

- (id) initWithFrame:(CGRect)frame andText : (NSString*) text
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setClipsToBounds:YES];

        animating = NO;
        needToUpdateText = NO;

        [self setScrollDuration:10.0];
        [self setScrollInterval:2.0];

        label1 = [UILabel new];
        label2 = [UILabel new];

        [label1 setBackgroundColor:[UIColor clearColor]];
        [label2 setBackgroundColor:[UIColor clearColor]];

        [self addSubview:label1];

        [self adjustLabelsForNewText:text andParentFrame:frame];        
    }

    return self;
}

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    if(needToUpdateText)
    {
        animating = NO;
        needToUpdateText = NO;
        [self adjustLabelsForNewText:@"" andParentFrame:[self frame]];
        [self adjustLabelsForNewText:newText andParentFrame:[self frame]];
        newText = nil;

        return;
    }

    [self setContentOffset:CGPointMake(0.0, 0.0)];
    [self beginScrolling];
}

- (void) beginScrolling
{
    animating = YES;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:[self scrollDuration]];
    [UIView setAnimationDelay:[self scrollInterval]];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];

    [self setContentOffset:CGPointMake(textWidth, 0.0)];

    [UIView commitAnimations];
}

- (BOOL) textFitsInFrame
{
    return textWidth <= [self frame].size.width;
}

- (void) setText:(NSString *)text
{
    if(animating)
    {
        newText = text;
        needToUpdateText = YES;
    }
    else 
    {
        [self adjustLabelsForNewText:text andParentFrame:[self frame]];
    }
}
和我恋爱吧 2024-10-26 16:23:01

如果你使用这一行在标签中出现完整的字符串
CGSize labelsize = [label.text sizeWithFont:label.font];
然后在 CGRectMark(10,10, label.width,100 );

选取框基于滚动内容大小,如果您设置内容大小(200,300),则表示将该值与 x 和 y 值进行比较来设置选取框。

看待
CN西瓦库玛

if u appear a complete string in label using this line
CGSize labelsize = [label.text sizeWithFont:label.font];
then in CGRectMark(10,10, label.width,100 );

marquee are based on scroll content-size,if u set content-size(200,300),mean take that value to compare with x and y value to set a marquee.

regard
CNSivakumar

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文