MacOS 开发 — Dock 显示网速/消息

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/HeroGuo_JP/article/details/88111102

MacOS 开发 -Dock 显示网速/消息

实际应用中迅雷或者QQ类似的软件,在下载的时候会在Dock上显示下载的网速,或者显示消息个数等等?其实核心代码如下。动态的自己加就行了。

Objective-C

新建CImage类 继承自NSImageView

#import <Cocoa/Cocoa.h>

NS_ASSUME_NONNULL_BEGIN

@interface CImage : NSImageView

@property(nonatomic,strong)NSString *prog;

@end

NS_ASSUME_NONNULL_END
- (instancetype)init
{
    self = [super init];
    if (self) {
        _prog = @"0KB/S";
    }
    return self;
}

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    [[NSColor darkGrayColor] setFill];
    NSRect pathRect = NSMakeRect(0, 0, 128, 50);
    
    
    NSBezierPath *path = [[NSBezierPath alloc]init];
    [path appendBezierPathWithRoundedRect:pathRect xRadius:25 yRadius:25];
    [path fill];
    
    NSMutableAttributedString *acon = [[NSMutableAttributedString alloc]initWithString:_prog];
    [acon addAttribute:@"foregroundColor" value:NSColor.whiteColor range:NSMakeRange(0,acon.length)];
    [acon addAttribute:@"font" value:[NSFont systemFontOfSize:30] range:NSMakeRange(0,acon.length)];
    NSSize maxSize = NSMakeSize(128,50);
    
    NSRect aconRect = [acon boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
    [acon drawAtPoint:NSMakePoint(64 -aconRect.size.width/2, 25-aconRect.size.height/2)];
    
}

Controller中直接调用即可:

#import "CViewController.h"
#include "CImage.h"
@interface CViewController ()
@property(nonatomic,strong)CImage *downView;
@end

@implementation CViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _downView = [[CImage alloc]initWithFrame:NSMakeRect(0, 0, 128, 128)];
    [_downView setImage:[NSImage imageNamed:@"icon_beiwanglu"]];
     
     _downView.prog = @"2M/S";
    NSDockTile *dock =  NSApp.dockTile;
    dock.showsApplicationBadge = true;
    dock.badgeLabel = @"123";
    dock.contentView = _downView;
    
    [dock display];
}

@end

Swift

class CImage: NSImageView {

    var ws: String = "0KB/S"

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)

        NSColor.black.setFill()
        NSBezierPath.init(roundedRect: NSRect(x: 0, y: 0, width: 128, height: 50), xRadius: 25, yRadius: 25).fill()

        let acon: NSMutableAttributedString = NSMutableAttributedString(string: ws)
        acon.addAttribute(.foregroundColor, value: NSColor.white, range: NSMakeRange(0, acon.length))
        acon.addAttribute(.font, value: NSFont.systemFont(ofSize: 30), range: NSMakeRange(0, acon.length))

        let maxsize = NSMakeSize(128, 50)
        let aconRect = acon.boundingRect(with: maxsize, options: .usesLineFragmentOrigin, context: nil)

        acon.draw(at: NSMakePoint(64 - aconRect.width / 2, 25 - aconRect.height / 2))
    }

}
class ViewController: NSViewController {

    let downView: CImage = {
        let view = CImage(frame: CGRect(x: 0, y: 0, width: 128, height: 128))
        view.image = NSImage(imageLiteralResourceName: "icon_beiwanglu")

        return view
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        downView.ws = "2M/S"

        let dock = NSApp.dockTile
        dock.showsApplicationBadge = true
        dock.badgeLabel = "123" // 角标
        dock.contentView = downView
        dock.display()
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }

}

猜你喜欢

转载自blog.csdn.net/HeroGuo_JP/article/details/88111102