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

  • Objective-C
  • Swift

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

Objective-C

新建CImage类 继承自NSImageView

#import <Cocoa/Cocoa.h>NS_ASSUME_NONNULL_BEGIN@interface CImage : NSImageView@property(nonatomic,strong)NSString *prog;@endNS_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.dockTiledock.showsApplicationBadge = truedock.badgeLabel = "123" // 角标dock.contentView = downViewdock.display()}override var representedObject: Any? {didSet {// Update the view, if already loaded.}}}

MacOS 开发 — Dock 显示网速/消息相关推荐

  1. mac显示网速_Mac系统怎么查看测试网速

    测试电脑网速的方法很多,有些人对MAC的操作不是很熟悉,不会用MAC测试网速.苹果电脑怎么查网速这个问题在网上大把的人询问.下面小编就教大家如何测试MAC的网速. 1.安装系统助手后即可在菜单栏看到网 ...

  2. linux状态栏显示命令,Ubuntu状态栏显示网速CPU

    作为系统操作的重要操作参数,网速和CPU占用等等,一直使用者所关心的.尤其是在需要下载或是上传文件的时候,对网速的关注可能会超出自己的想象,一直想要监视着网速. 所以,就找找能够在ubuntu状态栏上 ...

  3. Unity 最新UnityWebRequest下载,同时显示下载进度,和 显示网速,今天贴出来和大家分享

    Unity 最新UnityWebRequest下载网络资源,支持断点续传.多文件同时下载,同时显示下载进度,和 显示网速,今天贴出来和大家分享 显示网速图片 附上案例链接 可下载 https://do ...

  4. ubuntu实时显示网速cpu占用和内存占用率

    ubuntu实时显示网速cpu占用和内存占用率 大家在使用ubuntu的时候,有没有想让它实时显示网速,内存占用率,或者cpu占用率呢?现在我就教大家怎么实现,就像下面这样 1. 添加indicato ...

  5. TrafficMonitor win7 win10 任务栏显示 网速 CPU利用率

    TrafficMonitor win7 win10 任务栏显示 网速 CPU利用率 1,TrafficMonitor 安装 1,TrafficMonitor 安装 显示当前网速.CPU及内存利用率的桌 ...

  6. iStat Menus ——mac上显示网速的软件下载地址及注册码

    在mac上显示网速的软件--iStat Menus 5: https://bjango.com/mac/istatmenus/ 注册码: Email: 982092332@qq.com SN: GAW ...

  7. Python实现桌面悬浮窗(显示网速,内存,CPU)

    Python实现桌面悬浮窗(显示网速,内存,CPU) 背景介绍 编程环境 UI窗口设计 设置窗口透明与边框 重写鼠标事件,以拖动窗口 实现贴边收起 获取网速,内存,CPU信息 建立QThread线程, ...

  8. ubuntu18.04实时显示网速CPU温度等

    网上很多使用GNOME Shell integration 插件,但是本人还是喜欢以前的sysmonitor来显示网速 CPU温度等 1.添加源 sudo add-apt-repository ppa ...

  9. 在mac上显示网速的软件——iStat Menus 5:

    在mac上显示网速的软件--iStat Menus 5:  https://bjango.com/mac/istatmenus/ 注册码:  Email: 982092332@qq.com  SN: ...

  10. 电脑任务栏同时显示网速与CPU和内存的占用。

    需要下载一个轻量级工具TrafficMonitor,大小仅占1.1M 下载地址一:https://github.com/zhongyang219/TrafficMonitor 下载地址二:https: ...

最新文章

  1. 汇编:SF符号标志位
  2. Sublime Text 3 全套快捷键及功能介绍
  3. OpenCV检测平面物体
  4. 传神成进博会唯一指定智能翻译硬件提供商 力助无障碍沟通
  5. 抖音ai智能机器人挂机_电销秘诀 电销企业难以拒绝的AI智能电销机器人
  6. 004-controller的使用
  7. java 类 解析_Java类详解
  8. Java基础学习总结(144)——String类为什么要被定义为不可变类
  9. bzoj 1716 找零钱
  10. oracle 数据管理,Oracle深入浅出之数据管理
  11. 程序员要么在变来变去中成长,要么在变来变去中被淘汰,要么主动去适应变来边去的事实...
  12. 【TSP】基于matlab遗传和模拟退火算法求解中国省会城市旅行商问题【含Matlab源码 1254期】
  13. 狂雨小说采集笔趣阁源码
  14. Eclipse ADT 进行android应用签名打包详解
  15. 深度:嵌入式系统的软件架构设计
  16. “CSIG 计算机视觉前沿 · 研讨会”于6月28日在百度举行
  17. 实验8.1 时间换算
  18. 华硕笔记本开机直接进入bios解决方法
  19. 如何利用数据挖掘让RTB广告效果倍增?
  20. 微信公众号文章信息(阅读量、在看、点赞数)获取

热门文章

  1. BZZ节点如何成为有效节点,BZZ节点优势
  2. Word如何设置添加文本框?分享经验!怎么在word中加入文本框?
  3. html自动排版 vs,Visual Stidio Code (vscode)自定义HTML页面自动生成格式
  4. 20.8 cace判断
  5. 使用Spring Cache设置缓存条件
  6. VIRTUALBOX无法加载USB移动设备的解决方法
  7. One-shot Face Reenactment(BMVC19)
  8. linux运行blast,Linux下BLAST的使用
  9. 科学健脑,让IQ更上一层楼
  10. 【C语言】案例四十九 学生档案管理系统