Sourcehunt-Swift Edition-2017年3月

tech2022-09-30  56

In continuation of Swift Month, this Sourcehunt is full of Swift goodness. We’ll show you packages for manipulating strings, creating re-usable styles, displaying user avatars, and bringing the awesomeness of FontAwesome to iOS. We’ve also got a package for parsing JSON like a boss, and user authentication for your next (or first!) Vapor app. Did I mention it’s all in Swift!

在Swift月的延续中,这个Sourcehunt充满了Swift的天赋。 我们将向您展示用于处理字符串,创建可重复使用的样式,显示用户头像以及将FontAwesome的出色功能带到iOS的软件包。 我们还提供了一个用于解析JSON(如boss)的软件包,并为您的下一个(或第一个!)蒸气应用程序提供了用户身份验证。 我有没有提到过所有这些内容!

ArtSabintsev /吉他 [350★] (ArtSabintsev/Guitar[350 ★])

Inspired by JavaScript’s Voca library, this package makes working with strings a cinch. It adds some helpful methods to the String class that let you do anything from change case to add padding.

受JavaScript的Voca库的启发,此软件包使处理字符串变得轻而易举。 它向String类添加了一些有用的方法,使您可以执行从更改大小写到添加填充的任何操作。

Let’s say I have an app that takes a filename as input from the user. I want to make sure that filename does not have any spaces or capitalized words in it. Thanks to Guitar, I can do this easily.

假设我有一个应用程序,它使用文件名作为用户输入。 我想确保文件名中没有空格或大写字母。 多亏了Guitar,我可以轻松做到这一点。

var filename = "Deathstar Plans.pdf" if !filename.isAlphanumeric() { filename = filename.snakeCased().lowercased() } print(filename) // -> deathstar_plans.pdf

There’s a lot more that this library can do, and there’s still some features yet to be implemented. So whether you need to do some string magic or just want to help out, look no further than Guitar!

这个库可以做的事情还很多,还有一些功能尚待实现。 因此,无论您是需要做一些弦魔术还是只想帮忙,吉他都是您的理想之选!



psharanda / Atributika [70★] (psharanda/Atributika[70 ★])

Sticking with strings for a moment, here is Atributika. Let’s say you’re writing a social app where your users can add hashtags anywhere in their posts. You would like to style those hashtags so that they stand out from the rest of the text. Atributika makes this simple. It allows you to define style rules for styling your strings. Below I’ve created a style rule for hashtags:

坚持一会儿,这里是Atributika。 假设您正在编写一个社交应用,用户可以在其帖子的任何位置添加主题标签。 您希望设置这些标签的样式,以便它们在文本的其余部分中脱颖而出。 Atributika使这一过程变得简单。 它允许您定义用于样式化字符串的样式规则。 下面,我为标签创建了样式规则:

let myString = "Vader told me he's my dad today! I'm totally freaking out! #confused #shouldastayedondagoba" let formattedText = myString.styleHashtags( Style.foregroundColor(Color.blue).font(.systemFont(ofSize: 15)) ).attributedString

which produces this:

产生这个:

Atributika can also parse custom HTML markup allowing you to easily style text and reuse styles throughout your app. Here’s an example of defining some custom styles with Atributika:

Atributika还可以解析自定义HTML标记,使您可以轻松设置文本样式并在整个应用程序中重复使用样式。 这是使用Atributika定义一些自定义样式的示例:

let e = Style.font(.italicSystemFont(ofSize: 14)) let hl = Style.backgroundColor(Color.green)

And here I can use my styles to produce a fancy attributed string full of wisdom:

在这里,我可以使用自己的样式来生成富有智慧的花式属性字符串:

let myString = "<e>Do</e>, or <e>do not</e>. <hl>There is no try.</hl>".style(tags: e, hl).attributedString

Which looks like this:

看起来像这样:

One way to better leverage the power of this library would be to define all your styles in one service object. This object could then be used as an attributed string factory that you pass strings through. This allows you to keep all your styles in one place and use them throughout your application.

更好地利用此库功能的一种方法是在一个服务对象中定义所有样式。 然后,可以将此对象用作传递字符串的属性字符串工厂。 这使您可以将所有样式保留在一个位置,并在整个应用程序中使用它们。



ayushn21 / AvatarImageView [173★] (ayushn21/AvatarImageView[173 ★])

This package is especially useful when working on a social application. AvatarImageView provides a special UIImageView for displaying user profile pictures. If the user does not have a picture, the AvatarImageView displays the user’s initials. All you need is a data source that conforms to the AvatarImageViewDataSource protocol, like so:

在社交应用程序上工作时,此软件包特别有用。 AvatarImageView提供了一个特殊的UIImageView来显示用户个人资料图片。 如果用户没有图片,则AvatarImageView显示用户的姓名缩写。 您需要的是一个符合AvatarImageViewDataSource协议的数据源,如下所示:

struct MyUser: AvatarImageViewDataSource { var name = "Han Solo" }

This object can specify several options such as an image, specific initials, a background color, etc…

该对象可以指定多个选项,例如图像,特定的缩写,背景色等。

Optionally, you can also include a configuration for things like the shape of the avatar like so:

(可选)您还可以为虚拟形象的形状添加配置,如下所示:

struct AvatarConfig: AvatarImageViewConfiguration { var shape: Shape = .square }

Then you can feed an AvatarImageView your data source and configuration like so:

然后,您可以为AvatarImageView数据源和配置,如下所示:

myAvatar?.configuration = AvatarConfig() myAvatar?.dataSource = MyUser()

And just like that, you have an avatar image!

就这样,您有了头像图片!

It may not seem like much, but this package eliminates the need to worry about falling back from a missing image, getting initials, reshaping images, etc…

它看起来似乎并不多,但是该软件包消除了担心丢失图像,取回首字母,重塑图像等问题的烦恼。



查尔斯·奥德/ FontAwesomeSwift [1★] (charles-oder/FontAwesomeSwift [1 ★])

This awesome library brings the awesomeness of FontAwesome to your next awesome swift project! (have I said awesome enough yet?) If you’re unfamiliar with FontAwesome, I would recommend checking it out. It’s a set of vector graphics and CSS that allows you to display and customize icons as if they were text. While the original FontAwesome is primarily for the web, this package brings that same simplicity and customization to iOS, WatchOS, MacOS, etc… Say goodbye to editing icons in Photoshop or GIMP.

这个很棒的库将FontAwesome的出色功能带入下一个很棒的Swift项目! (我说的还真棒吗?)如果您不熟悉FontAwesome,我建议您检查一下。 它是一组矢量图形和CSS,可让您像显示图标一样显示和自定义图标。 虽然原始的FontAwesome主要用于网络,但此软件包为iOS,WatchOS,MacOS等带来了相同的简单性和自定义性。告别在Photoshop或GIMP中编辑图标。

The API is simple: create an FASIcon object, style it, and put it where you want it. You can convert your FASIcon into a UIImage or NSAttributedString as well. Here are a couple of examples.

该API很简单:创建一个FASIcon对象,设置其样式,然后将其放在所需的位置。 您也可以将FASIcon转换为UIImage或NSAttributedString 。 这里有几个例子。

First I’ll make a large UIImage from a FontAwesome icon:

首先,我将从FontAwesome图标制作一个大的UIImage:

myImageView?.image = FASFontAwesome().userCircleIcon(size: 32).color(color: UIColor.blue).image

Next, I’ll create an attributed string with some icons mixed in for fun

接下来,我将创建一个属性字符串,其中包含一些有趣的图标

let home: NSAttributedString = FASFontAwesome().homeIcon(size: 17).attributedString let wifi: NSAttributedString = FASFontAwesome().wifiIcon(size: 17).color(color: UIColor.green).attributedString let myString: NSMutableAttributedString = NSMutableAttributedString() myString.append(home) myString.append(NSAttributedString(string: " is where the ", attributes: nil)) myString.append(wifi) myString.append(NSAttributedString(string: " connects automatically.", attributes: nil)) myLabel?.attributedText = myString

And here is the end result!

这就是最终的结果!

Despite its name, this package actually supports several different icon packs such as Material, Ion, and much more. You can even extend it with your own icons!

尽管名称如此,但该程序包实际上支持几种不同的图标包,例如Material,Ion等。 您甚至可以使用自己的图标对其进行扩展!



节点iOS /蛇 [70★] (nodes-ios/Serpent[70 ★])

Next up is a very polished and efficient JSON parser called Serpent. This package is seriously cool. Not only is it extremely fast (they have stats to prove it), but the creators have worked hard to make it very easy to use.

接下来是一个非常优美且高效的JSON解析器,称为Serpent。 这个包装真的很酷。 它不仅非常快(他们有统计数据来证明这一点),而且创作者们都在努力使它非常易于使用。

Let’s say I have a struct shaped like this:

假设我有一个像这样的struct :

struct StormTrooper { var name = "FN93847" var favoriteColor = "white" var shootingRangeScore = "2/457" }

And I want to pull some StormTroopers from an API. The first thing I must do is teach Serpent what my data looks like and how I’d like it parsed or serialized. To do this, my struct must implement the Serializable protocol, which comes bundled with Serpent. This protocol requires the following methods for data mapping:

我想从API中提取一些StormTroopers 。 我必须做的第一件事是教Serpent我的数据是什么样子以及如何解析或序列化它。 为此,我的struct必须实现与Serpent捆绑在一起的Serializable协议。 该协议需要以下方法进行数据映射:

extension StormTrooper: Serializable { init(dictionary: NSDictionary?) { name <== (self, dictionary, "name") favoriteColor <== (self, dictionary, "favorite_color") shootingRangeScore <== (self, dictionary, "shooting_range_score") } func encodableRepresentation() -> NSCoding { let dict = NSMutableDictionary() (dict, "name") <== name (dict, "favorite_color") <== favoriteColor (dict, "shooting_range_score") <== shootingRangeScore return dict } }

Now, this is just for three attributes. Writing all this code for larger structures is going to be tedious and time-consuming. Thankfully, the team behind Serpent wrote a small Mac application called Model Boiler that generates this code for you. To use it, copy your struct to the system clipboard and hit cmd+option+6. This populates your clipboard with all the generated code for that struct. Then you can paste it where you want it and make any tweaks if necessary.

现在,这仅用于三个属性。 为较大的结构编写所有这些代码将是乏味且耗时的。 值得庆幸的是,Serpent背后的团队编写了一个名为Model Boiler的小型Mac应用程序,它可以为您生成此代码。 要使用它,请将您的struct复制到系统剪贴板,然后单击cmd + option + 6 。 这将使用该struct所有生成的代码填充剪贴板。 然后,您可以将其粘贴到所需的位置,并在必要时进行任何调整。



风暴路径/旋转栅 [95★] (stormpath/Turnstile[95 ★])

Finally, if you’re a server-side swifter using Vapor, you’ll find Turnstile particularly interesting. The folks at Stormpath wrote it to manage user authentication in Vapor apps. It uses several objects to track the current user in the application and ensure they are authenticated. It also provides objects for storing the user’s credentials in memory. If you want to store the credentials elsewhere, you can write custom objects called Realms to do so.

最后,如果您是使用Vapor的服务器端工具,您会发现Turnstile特别有趣。 Stormpath的员工编写了该文件,以管理Vapor应用程序中的用户身份验证。 它使用多个对象来跟踪应用程序中的当前用户,并确保对它们进行身份验证。 它还提供用于在内存中存储用户凭据的对象。 如果要将凭据存储在其他位置,则可以编写称为Realms自定义对象。

Turnstile also facilitates logging through third parties such as Facebook and Google. It’s a robust library written by people who understand authentication.

Turnstile还有助于通过Facebook和Google等第三方进行登录。 这是一个由了解身份验证的人编写的健壮的库。



Each of these great packages can be extremely useful, so go out there and give them some love. Even better, go dig into their source code and submit some PRs. Whether you’re looking for some new tools or a new project to contribute to, you’ve got a great list here. If you’ve found other great libraries that you’d like to share, let us know using the #sourcehunt and #swift tags!

这些出色的套件中的每一个都可能非常有用,因此请出去给他们一些爱。 更好的是,深入研究其源代码并提交一些PR。 无论您是要寻找一些新工具还是一个新项目来贡献自己,这里都有一个不错的清单。 如果您找到了其他想要共享的出色库,请使用#sourcehunt和#swift标签让我们知道!

翻译自: https://www.sitepoint.com/sourcehunt-swift-edition-march-2017/

最新回复(0)