iTunes歌曲 [英] iTunes Song Title Scrolling in Cocoa
问题描述
我进行了广泛的搜索,不能为我的生活找到任何有关如何实现类似效果的iTunes歌曲标题滚动,如果文本太大的Cocoa。我已经尝试设置NSTextField的边界无效。我已经尝试使用NSTextView以及各种尝试使用NSScrollView。我相信我错过了一些简单的东西,但任何帮助将非常感谢。我也希望不要使用CoreGraphics,如果可能的话。
I have searched extensively and cannot for the life of me find any information about how to achieve a similar effect to that of the iTunes song title scrolling if the text is too large in Cocoa. I have tried setting the bounds on a NSTextField to no avail. I have tried using NSTextView as well as various attempts at using NSScrollView. I am sure I am missing something simple but any help would be greatly appreciated. I am also hoping to not have to use CoreGraphics if at all possible.
示例,请注意Base.FM http:// www 。文本已滚动。如果你需要一个更好的例子打开iTunes与一个相当大的标题的歌曲,并观看它来回滚动。
Example, notice the "Base.FM http://www." text has been scrolled. If you need a better example open iTunes with a song with a rather large title and watch it scroll back and forth.
我认为有一个简单的方法
I would think surely there is a simple way to just create a marquee type effect with an NSTextField and an NSTimer, but alas.
推荐答案
我可以看到这将是很困难,如果你是一个NSTextField和NSTimer, 试图将功能集成到现有的控制中。但是,如果你只是开始一个纯NSView,这不是那么糟糕。我在大约10分钟后就鞭打这个...
I can see how this would be difficult if you're trying to shoehorn the functionality into an exist control. However, if you just start with a plain NSView, it's not that bad. I whipped this up in about 10 minutes...
//ScrollingTextView.h:
#import <Cocoa/Cocoa.h>
@interface ScrollingTextView : NSView {
NSTimer * scroller;
NSPoint point;
NSString * text;
NSTimeInterval speed;
CGFloat stringWidth;
}
@property (nonatomic, copy) NSString * text;
@property (nonatomic) NSTimeInterval speed;
@end
//ScrollingTextView.m
#import "ScrollingTextView.h"
@implementation ScrollingTextView
@synthesize text;
@synthesize speed;
- (void) dealloc {
[text release];
[scroller invalidate];
[super dealloc];
}
- (void) setText:(NSString *)newText {
[text release];
text = [newText copy];
point = NSZeroPoint;
stringWidth = [newText sizeWithAttributes:nil].width;
if (scroller == nil && speed > 0 && text != nil) {
scroller = [NSTimer scheduledTimerWithTimeInterval:speed target:self selector:@selector(moveText:) userInfo:nil repeats:YES];
}
}
- (void) setSpeed:(NSTimeInterval)newSpeed {
if (newSpeed != speed) {
speed = newSpeed;
[scroller invalidate];
scroller == nil;
if (speed > 0 && text != nil) {
scroller = [NSTimer scheduledTimerWithTimeInterval:speed target:self selector:@selector(moveText:) userInfo:nil repeats:YES];
}
}
}
- (void) moveText:(NSTimer *)timer {
point.x = point.x - 1.0f;
[self setNeedsDisplay:YES];
}
- (void)drawRect:(NSRect)dirtyRect {
// Drawing code here.
if (point.x + stringWidth < 0) {
point.x += dirtyRect.size.width;
}
[text drawAtPoint:point withAttributes:nil];
if (point.x < 0) {
NSPoint otherPoint = point;
otherPoint.x += dirtyRect.size.width;
[text drawAtPoint:otherPoint withAttributes:nil];
}
}
@end
在Interface Builder中将NSView拖动到您的窗口上,并将其类更改为ScrollingTextView。然后(在代码中),你做:
Just drag an NSView onto your window in Interface Builder and change its class to "ScrollingTextView". Then (in code), you do:
[myScrollingTextView setText:@"This is the text I want to scroll"];
[myScrollingTextView setSpeed:0.01]; //redraws every 1/100th of a second
这显然很基础,周围的东西,你正在寻找,是一个合适的地方开始。
This is obviously pretty rudimentary, but it does the wrap around stuff that you're looking for and is a decent place to start.
这篇关于iTunes歌曲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!