2

我有一个标签和里面的文字。我希望我的文本像数字信息板一样跨越标签的宽度。如何在 iOS 中做到这一点?我尝试使用该代码(我从这里获得:http ://www.youtube.com/watch?v=EFoNEjPwTXM ),但它不起作用:

在 .m 文件中:

-(void)time: (NSTimer *) theTimer
{
    currentSong.center = CGPointMake(currentSong.center.x - 3.5, currentSong.center.y);
    if (currentSong.center.x < - (currentSong.bounds.size.width/2))
    {
        currentSong.center = CGPointMake (320 + (currentSong.bounds.size.width/2), currentSong.center.y);
    }
}

viewDidLoad

timer = [NSTimer timerWithTimeInterval:0.09 target:self selector:@selector(time:) userInfo:nil repeats:YES];

在 .h 文件中:

    IBOutlet UILabel *currentSong;
    IBOutlet NSTimer *timer;    

-(void)time: (NSTimer *) theTimer;

@end
4

4 回答 4

0
- (void)sendNotification:(NSString*)txt{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:3.5];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:messageView cache:YES];
    [lblTime setText:txt];
    [lblTime setTextAlignment:UITextAlignmentLeft];
    lblTime.frame = CGRectMake(260, 0, 258, 19);
    [UIView commitAnimations];
}
于 2013-05-23T11:57:04.217 回答
0

试试这个...它可能满足你的要求

 int x;
float size;
@synthesize label;
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    label=[[UILabel alloc]init ];
           //WithFrame:CGRectMake(300, 400, 400, 30)];
    label.text=@"www.stackoverflow.com";
    size=[self getLabelWidth:label];
    label.frame=CGRectMake(300, 400, size, 30);

    label.backgroundColor=[UIColor clearColor];
    x=300;

    [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(clickme) userInfo:nil repeats:YES];

}

-(void)clickme
{
    x--;
    label.frame=CGRectMake(x, 400, size, 30);
    if( x==-size)
    {
        x=300;
    }
    [self.view addSubview:label];
}

-(float)getLabelWidth:(UILabel*)label1
{

    CGSize maximumSize = CGSizeMake(500,30);
    CGSize StringSize = [label1.text sizeWithFont:label1.font constrainedToSize:maximumSize lineBreakMode:UILineBreakModeTailTruncation];
    NSLog(@"width is %f",StringSize.width);
    return StringSize.width;
}
于 2013-03-15T04:26:24.090 回答
0

试试这个可能会有所帮助

-(void)viewDidLoad {

    [super viewDidLoad];

    [self marqueeMessage:@"test"];
    // Do any additional setup after loading the view, typically from a nib.
}



- (void)marqueeMessage:(NSString *)messageString {
    UILabel *label = [[UILabel alloc] initWithFrame:(CGRectMake(0, 50, 90, 21))];
    //label.tag=nextIndex;
    label.text = messageString;
     label.backgroundColor=[UIColor clearColor];
    [self.view addSubview:label];
    [UIView beginAnimations:@"test" context:nil];
    [UIView setAnimationDuration:3];
    [UIView setAnimationDidStopSelector:@selector(marqueeMessage:)];
    [UIView setAnimationDelegate:self];

    label.frame = CGRectMake(360,50,90,21);
    [UIView commitAnimations];
}
于 2013-03-14T13:20:11.047 回答
0

这有点骇人听闻。它所做的只是在标签中已经存在的文本之前定期添加空格,因此文本看起来像是在移动,而实际上标签没有。这是代码:

//Interface (.h)
@property (nonatomic, strong) IBOutlet UILabel *label; //Remember to connect this to    
                                                       //a label in Storyboards

//Implementation (.m)
-(void)moveText {
    [self.label setText:[NSString stringWithFormat:@" %@", self.label.text]];
}

- (void)viewDidLoad {
    [self.label setText:@"A very long and boring string"];

    //You can change the time interval to change the speed of the animation
    [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(moveText) userInfo:nil repeats:YES];
}
于 2013-03-15T05:05:50.673 回答