用 20+ 行 JavaScript 代码,短暂“变身” iOS 程序员!

2022 年 9 月 7 日 CSDN

摘要:你有没有尝试过在 iOS 中创建小部件,感受一把身为 iOS 程序员的快乐本文作者将用二十几行 JavaScript 代码教你构建一个 iOS 小部件,据他所说,这“一点都不难

原文链接:https://stackonfire.com/feel-yourself-ios-developer-with-20-lines-of-javascript

声明:本文为 CSDN 翻译,未经授权,禁止转载。


作者
| Dimitri Ivashchuk
译者 | 弯月
出品 | CSDN(ID:CSDNnews)

在这篇入门指南中,我们将构建如下软件,实际所需编写的 JavaScript 代码非常少。

这个软件的创意来源有两个:

1.有人使用这个小部件来显示收入。

2.频繁检查 GitHub 代码库的星星数量很麻烦。

我发现,构建 iOS 小部件一点都不难,只要有合适的工具,再加上非常基础的 JavaScript 知识即可!


项目简介


这个项目是一个 iOS 小部件,用来显示 GitHub 代码库的星星数量。这里我通过自己的一个开源项目为例进行说明。我们将采用 Scriptable,这个工具可以通过 JavaScript 生成漂亮的小部件,就像上图一样。


代码


// Request// Nothing special here, just an async request function GitHub open APIasync function getGithubData() {const url = "https://api.github.com/repos/lost-pixel/lost-pixel";const request = new Request(url);const response = await request.loadJSON();return response;}//UI// Function that defines the element of the widgetasync function createWidget() {// Fetching data with the function we prepared beforeconst githubData = await getGithubData();// Create new wdiget & set black background colorlet listwidget = new ListWidget();listwidget.backgroundColor = new Color("#000000");// Create heading and style it properlylet heading = listwidget.addText("⭐ Lost Pixel ⭐");heading.centerAlignText();heading.font = Font.lightSystemFont(25);heading.textColor = new Color("#fff");// Add spacer between elementslistwidget.addSpacer(15);// Create the stars display and style it properly. We use the data from API herelet stars = listwidget.addText(githubData.stargazers_count);stars.centerAlignText();stars.font = Font.semiboldSystemFont(20);stars.textColor = new Color("#ffffff");return listWidget;}// Execute createWidget function that returns us the widgetlet widget = await createWidget();// Show the widget when added to IOS homescreenif (config.runsInWidget) {Script.setWidget(widget);} else {widget.presentMedium();}// finish the execution of the scriptScript.complete();

显示小部件


1.在 iOS 设备上安装 Scriptable for iOS;

2.点击 + 按钮创建一个新的脚本。

3. 将上面的代码粘贴到空白输入处。

4. 保存,这样就完成了!

5. 最后一步,只需将这个小部件添加到主屏幕即可:通过 Scriptable 小部件,将刚刚建好的小部件添加到主屏幕。

好了,恭喜你完成了!

— 推荐阅读 —
   
   
     
Node 之父斥责 Oracle:你们也不用,那请交出 JavaScript 商标!
苹果往事:“我们必须制造 iPhone,它一定会终结 iPod”
JavaScript 后端开发者必须跨的 10 道门槛!


登录查看更多
0

相关内容

JavaScript 是弱类型的动态脚本语言,支持多种编程范式,包括面向对象和函数式编程。
【2022新书】深度学习R语言实战,第二版,568页pdf
专知会员服务
81+阅读 · 2022年10月23日
【干货书】《Pydon'ts:编写优雅的Python代码》,263页pdf
专知会员服务
91+阅读 · 2021年11月2日
【干货书】Linux命令行与shell脚本编程大全,第3版818页pdf
专知会员服务
61+阅读 · 2020年12月30日
算法与数据结构Python,369页pdf
专知会员服务
160+阅读 · 2020年3月4日
【GitHub实战】Pytorch实现的小样本逼真的视频到视频转换
专知会员服务
35+阅读 · 2019年12月15日
编程语言Zig有什么与众不同的
InfoQ
0+阅读 · 2022年11月9日
手把手教你写 Dart ffi
阿里技术
0+阅读 · 2022年11月7日
让程序员早点下班的《技术写作指南》
极市平台
2+阅读 · 2022年7月16日
JavaScript 中的 7 个“杀手级”单行代码
InfoQ
0+阅读 · 2022年3月17日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2013年12月31日
国家自然科学基金
0+阅读 · 2012年12月31日
国家自然科学基金
0+阅读 · 2012年12月31日
国家自然科学基金
0+阅读 · 2012年12月31日
国家自然科学基金
0+阅读 · 2011年12月31日
国家自然科学基金
0+阅读 · 2009年12月31日
国家自然科学基金
0+阅读 · 2009年12月31日
Arxiv
0+阅读 · 2022年11月23日
Arxiv
0+阅读 · 2022年11月23日
VIP会员
相关基金
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2013年12月31日
国家自然科学基金
0+阅读 · 2012年12月31日
国家自然科学基金
0+阅读 · 2012年12月31日
国家自然科学基金
0+阅读 · 2012年12月31日
国家自然科学基金
0+阅读 · 2011年12月31日
国家自然科学基金
0+阅读 · 2009年12月31日
国家自然科学基金
0+阅读 · 2009年12月31日
Top
微信扫码咨询专知VIP会员