7

私は広範囲にわたって検索しましたが、Cocoaでテキストが大きすぎる場合に、iTunesの曲のタイトルのスクロールと同様の効果を実現する方法についての情報を見つけることができません。NSTextFieldの境界を無駄に設定しようとしました。NSTextViewの使用と、NSScrollViewのさまざまな使用を試みました。私は何か簡単なものが欠けていると確信していますが、どんな助けでも大歓迎です。また、可能な限りCoreGraphicsを使用する必要がないことを望んでいます。

として、「Base.FM http://www 」に注目してください。テキストがスクロールされました。より良い例が必要な場合は、タイトルがかなり大きい曲でiTunesを開き、前後にスクロールするのを見てください。

NSTextFieldとNSTimerを使ってマーキータイプのエフェクトを作成する簡単な方法は確かにあると思いますが、残念ながら。

4

2 に答える 2

20

機能を既存のコントロールに押し込もうとしている場合、これがどのように難しいかがわかります。ただし、単純なNSViewから始める場合は、それほど悪くはありません。私はこれを約10分で泡立てました...

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

NSViewをInterfaceBuilderのウィンドウにドラッグし、そのクラスを「ScrollingTextView」に変更するだけです。次に(コードで)、次のようにします。

[myScrollingTextView setText:@"This is the text I want to scroll"];
[myScrollingTextView setSpeed:0.01]; //redraws every 1/100th of a second

これは明らかにかなり初歩的なことですが、あなたが探しているものをラップアラウンドし、開始するのに適切な場所です。

于 2010-07-13T02:34:33.190 に答える
0

Swift 4でこれを探している人のために、私はDaveの答えを変換し、さらにいくつかの機能を追加しました。

import Cocoa

open class ScrollingTextView: NSView {
    /// Text to scroll
    open var text: NSString?

    /// Font for scrolling text
    open var font: NSFont?

    /// Scrolling text color
    open var textColor: NSColor = .headerTextColor

    /// Determines if the text should be delayed before starting scroll
    open var isDelayed: Bool = true

    /// Spacing between the tail and head of the scrolling text
    open var spacing: CGFloat = 20

    /// Amount of time the text is delayed before scrolling
    open var delay: TimeInterval = 2 {
        didSet {
            updateTraits()
        }
    }

    /// Length of the scrolling text view
    open var length: CGFloat = 0 {
        didSet {
            updateTraits()
        }
    }

    /// Speed at which the text scrolls. This number is divided by 100.
    open var speed: Double = 4 {
        didSet {
            updateTraits()
        }
    }

    private var timer: Timer?
    private var point = NSPoint(x: 0, y: 0)
    private var timeInterval: TimeInterval?

    private(set) var stringSize = NSSize(width: 0, height: 0) {
        didSet {
            point.x = 0
        }
    }

    private var timerSpeed: Double? {
        return speed / 100
    }

    private lazy var textFontAttributes: [NSAttributedString.Key: Any] = {
        return [NSAttributedString.Key.font: font ?? NSFont.systemFont(ofSize: 14)]
    }()

    /**
     Sets up the scrolling text view
     - Parameters:
     - string: The string that will be used as the text in the view
     */
    open func setup(string: String) {
        text = string as NSString
        stringSize = text?.size(withAttributes: textFontAttributes) ?? NSSize(width: 0, height: 0)
        setNeedsDisplay(NSRect(x: 0, y: 0, width: frame.width, height: frame.height))
        updateTraits()
    }
}

private extension ScrollingTextView {
    func setSpeed(newInterval: TimeInterval) {
        clearTimer()
        timeInterval = newInterval

        guard let timeInterval = timeInterval else { return }
        if timer == nil, timeInterval > 0.0, text != nil {
            if #available(OSX 10.12, *) {
                timer = Timer.scheduledTimer(timeInterval: newInterval, target: self, selector: #selector(update(_:)), userInfo: nil, repeats: true)

                guard let timer = timer else { return }
                RunLoop.main.add(timer, forMode: .commonModes)
            } else {
                // Fallback on earlier versions
            }
        } else {
            clearTimer()
            point.x = 0
        }
    }

    func updateTraits() {
        clearTimer()

        if stringSize.width > length {
            guard let speed = timerSpeed else { return }
            if #available(OSX 10.12, *), isDelayed {
                timer = Timer.scheduledTimer(withTimeInterval: delay, repeats: false, block: { [weak self] timer in
                    self?.setSpeed(newInterval: speed)
                })
            } else {
                setSpeed(newInterval: speed)
            }
        } else {
            setSpeed(newInterval: 0.0)
        }
    }

    func clearTimer() {
        timer?.invalidate()
        timer = nil
    }

    @objc
    func update(_ sender: Timer) {
        point.x = point.x - 1
        setNeedsDisplay(NSRect(x: 0, y: 0, width: frame.width, height: frame.height))
    }
}

extension ScrollingTextView {
    override open func draw(_ dirtyRect: NSRect) {
        if point.x + stringSize.width < 0 {
            point.x += stringSize.width + spacing
        }

        textFontAttributes[NSAttributedString.Key.foregroundColor] = textColor
        text?.draw(at: point, withAttributes: textFontAttributes)

        if point.x < 0 {
            var otherPoint = point
            otherPoint.x += stringSize.width + spacing
            text?.draw(at: otherPoint, withAttributes: textFontAttributes)
        }
    }

    override open func layout() {
        super.layout()
        point.y = (frame.height - stringSize.height) / 2
    }
}

参照の要点: https ://gist.github.com/NicholasBellucci/b5e9d31c47f335c36aa043f5f39eedb2

于 2018-11-13T05:08:48.800 に答える