John David

John David

1565240584

CSS :empty Selector

Often, we want to style elements that contain content. How about when an element has no children or text at all? Easy, you can use the :empty selector 🤩

<p> </p><!-- NOT empty: note the blank space -->
<p></p><!-- YES empty: nothing inbetween -->


p::before {
 font-family: "FontAwesome";
 content: "\f240";
}

p:empty::before {
 content: “\f244”;
}
 
p {
 color: silver;
}

p:empty {
 color: red;
}

What’s considered empty?

When I first encounter this, there was a few confusion about what this property considers as empty. Let’s stick with MDN’s definition here:

The :empty CSS pseudo-class represents any element that has no children. Children can be either element nodes or text (including whitespace). Comments, processing instructions, and CSS content do not affect whether an element is considered empty.

Is empty

As long as there is no whitespace, it’s an empty element.

<p></p>

Comment in between is also considered an empty element. As long as there is no whitespace.

<p><!-- comment --></p>

Not empty

Whitespace is considered not empty. Even in a new line, there is whitespace, so not empty! Emphasizing on this, cause I made the same mistake 😅

<p> </p>

<p>
<!-- comment -->
</p>

Having children element also counts as not empty

<p><span></span></p>

Whitespace in Future Spec

The good news is – in Selectors Level 4, whitespace would be considered empty! This will make it similar to act like :-moz-only-whitespace. In other words, this would considered empty:

<!-- Considered Empty in CSS Selectors Level 4 -->
<p> </p>

⚠️ BUT, don’t do this yet. Currently no browser supports this.

Examples using :empty

Okay, let’s take a look at some real-life examples of using :empty.

Using :empty in Form Error Message

This is the example that made me first discovered :empty. So I wanted to prepend a ❌ icon on my error message. But the problem is the icon appeared even when I had no error message. But then no problem, I can just use the :empty to only append the icon when there IS an error message 👍

CSS

.error:before {
 color: red;
 content: "\0274c "; /* ❌ icon */
}

HTML

<!-- No error message -->
<div class=“error”></div>

<!-- Yes error message -->
<div class=“error”>Missing Email</div>

Output

Without Empty

❌
❌ Missing Email

With :empty

❌ Missing Email

Using :empty in Alerts

Here’s another example using :empty to hide empty state.

.alert {
 background: pink;
 padding: 10px;
}
.alert:empty {
 display: none;
}

HTML

<div class=“alert”></div>
<div class=“alert”>Alert Message</div>

Output

Without empty
Alert Message
With :empty
Alert Message

Browser Support

Support on this is actually really good. It supports all the way to Internet Explorer 9 🙌

Community Examples

I discovered :empty when I was trying to style empty error messages in a form. <div class=“error”></div>. No JS required anymore, I can use pure CSS 👍

What other use cases do you know?

  • @delusioninabox: A fix to remove janky spacing of empty paragraph tags, generally from user-created content. 😅
  • @hkfoster: I’ve used it to squash any number of randomly generated selectors that I can’t weed out on the templating side. 👍
  • @sumurai8: Removing padding from empty paragraphs
  • @_ottenga: Nice for notification dot (if empty is not visible, if has some number - for example - is red with the number inside)
  • @stephenjbell: li:empty { visibility:hidden; } Let an empty list item act as kind of a paragraph break (no bullet) in the middle of a list.
  • @jlabs: I’ve used this recently within ul that show’s a message when the ul has no children - the list was populated via JS.

Community Input

  • @bourhaouta: One more thing about :empty it doesn’t select elements that contain whitespace, in the other side we have :blank but it’s not well supported yet 😔
  • @link2twenty: I love seeing little known features like this getting some spotlight! It’s probably worth noting when the level 4 selectors roll out white space will be included as empty 🙂

Thanks for reading ❤

If you liked this post, share it with all of your programming buddies!

Follow me on Facebook | Twitter

Learn More

☞ The Web Developer Bootcamp

☞ Build Responsive Real World Websites with HTML5 and CSS3

☞ Advanced CSS and Sass: Flexbox, Grid, Animations and More!

☞ Web Design for Beginners: Real World Coding in HTML & CSS

☞ HTML5 and CSS3 Fundamentals: Development for Absolute Beginners

☞ How to build and send a HTML Email 2019

☞ Using Web Workers to Real-time Processing

☞ How to build Desktop Apps with HTML, CSS and Javascript ?

☞ 34 Free HTML And CSS Books

☞ Building a Simple URL Shortener With HTML and Javascript

#css

What is GEEK

Buddha Community

CSS :empty Selector
Corey Brooks

Corey Brooks

1657254050

Top 9+ Common CSS Mistakes To Avoid

In this tutorial, we'll summarise what the top 9+ CSS mistakes are and how to avoid them.

Top 9+ Common CSS Mistakes To Avoid

It’s easy to get tripped up with CSS. Here are some common CSS mistakes we all make.

1. Not Using a Proper CSS Reset

Web browsers are our fickle friends. Their inconsistencies can make any developer want to tear their hair out. But at the end of the day, they’re what will present your website, so you better do what you have to do to please them.

One of the sillier things browsers do is provide default styling for HTML elements. I suppose you can’t really blame them: what if a “webmaster” chose not to style their page? There has to be a fallback mechanism for people who choose not to use CSS.

In any case, there’s rarely a case of two browsers providing identical default styling, so the only real way to make sure your styles are effective is to use a CSS reset. What a CSS reset entails is resetting (or, rather, setting) all the styles of all the HTML elements to a predictable baseline value. The beauty of this is that once you include a CSS reset effectively, you can style all the elements on your page as if they were all the same to start with.

It’s a blank slate, really. There are many CSS reset codebases on the web that you can incorporate into your work. I personally use a modified version of the popular Eric Meyer reset and Six Revisions uses a modified version of YUI Reset CSS.

You can also build your own reset if you think it would work better. What many of us do is utilizing a simple universal selector margin/padding reset.

* { margin:0; padding:0; } 

Though this works, it’s not a full reset.

You also need to reset, for example, borders, underlines, and colors of elements like list items, links, and tables so that you don’t run into unexpected inconsistencies between web browsers. Learn more about resetting your styles via this guide: Resetting Your Styles with CSS Reset.

2. Over-Qualifying Selectors

Being overly specific when selecting elements to style is not good practice. The following selector is a perfect example of what I’m talking about:

ul#navigation li a { ... } 

Typically the structure of a primary navigation list is a <ul> (usually with an ID like #nav or #navigation) then a few list items (<li>) inside of it, each with its own <a> tag inside it that links to other pages.

This HTML structure is perfectly correct, but the CSS selector is really what I’m worried about. First things first: There’s no reason for the ul before #navigation as an ID is already the most specific selector. Also, you don’t have to put li in the selector syntax because all the a elements inside the navigation are inside list items, so there’s no reason for that bit of specificity.

Thus, you can condense that selector as:

#navigation a { ... } 

This is an overly simplistic example because you might have nested list items that you want to style differently (i.e. #navigation li a is different from #navigation li ul li a); but if you don’t, then there’s no need for the excessive specificity.

I also want to talk about the need for an ID in this situation. Let’s assume for a minute that this navigation list is inside a header div (#header). Let us also assume that you will have no other unordered list in the header besides the navigation list.

If that is the case, we can even remove the ID from the unordered list in our HTML markup, and then we can select it in CSS as such:

#header ul a { ... } 

Here’s what I want you to take away from this example: Always write your CSS selectors with the very minimum level of specificity necessary for it to work. Including all that extra fluff may make it look more safe and precise, but when it comes to CSS selectors, there are only two levels of specificity: specific, and not specific enough.

3. Not Using Shorthand Properties

Take a look at the following property list:

#selector { margin-top: 50px; margin-right: 0; margin-bottom: 50px; margin-left 0; }

What is wrong with this picture? I hope that alarm bells are ringing in your head as you notice how much we’re repeating ourselves. Fortunately, there is a solution, and it’s using CSS shorthand properties.

The following has the same effect as the above style declaration, but we’ve reduced our code by three lines.

#selector { margin: 50px 0; }

Check out this list of properties that deals with font styles:

font-family: Helvetica; font-size: 14px; font-weight: bold; line-height: 1.5;

We can condense all that into one line:

font: bold 14px/1.5 Helvetica; 

We can also do this for background properties. The following:

background-image: url(background.png); background-repeat: repeat-y; background-position: center top;

Can be written in shorthand CSS as such:

background: url(background.png) repeat-y center top; 

4. Using 0px instead of 0

Say you want to add a 20px margin to the bottom of an element. You might use something like this:

#selector { margin: 20px 0px 20px 0px; } 

Don’t. This is excessive.

There’s no need to include the px after 0. While this may seem like I’m nitpicking and that it may not seem like much, when you’re working with a huge file, removing all those superfluous px can reduce the size of your file (which is never a bad thing).

5. Using Color Names Instead of Hexadecimal

Declaring red for color values is the lazy man’s #FF0000. By saying:

color: red;

You’re essentially saying that the browser should display what it thinks red is. If you’ve learned anything from making stuff function correctly in all browsers — and the hours of frustration you’ve accumulated because of a stupid list-bullet misalignment that can only be seen in IE7 — it’s that you should never let the browser decide how to display your web pages.

Instead, you should go to the effort to find the actual hex value for the color you’re trying to use. That way, you can make sure it’s the same color displayed across all browsers. You can use a color cheatsheet that provides a preview and the hex value of a color.

This may seem trivial, but when it comes to CSS, it’s the tiny things that often lead to the big gotchas.

6. Redundant Selectors

My process for writing styles is to start with all the typography, and then work on the structure, and finally on styling all the colors and backgrounds. That’s what works for me. Since I don’t focus on just one element at a time, I commonly find myself accidentally typing out a redundant style declaration.

I always do a final check after I’m done so that I can make sure that I haven’t repeated any selectors; and if I have, I’ll merge them. This sort of mistake is fine to make while you’re developing, but just try to make sure they don’t make it into production.

7. Redundant Properties

Similar to the one above, I often find myself having to apply the same properties to multiple selectors. This could be styling an <h5> in the header to look exactly like the <h6> in the footer, making the <pre>‘s and <blockquote>‘s the same size, or any number of things in between. In the final review of my CSS, I will look to make sure that I haven’t repeated too many properties.

For example, if I see two selectors doing the same thing, such as this:

#selector-1 { font-style: italic; color: #e7e7e7; margin: 5px; padding: 20px } .selector-2 { font-style: italic; color: #e7e7e7; margin: 5px; padding: 20px }

I will combine them, with the selectors separated by a comma (,):

#selector-1, .selector-2 { font-style: italic; color: #e7e7e7; margin: 5px; padding: 20px }

I hope you’re seeing the trend here: Try to be as terse and as efficient as possible. It pays dividends in maintenance time and page-load speed.

8. Not Providing Fallback Fonts

In a perfect world, every computer would always have every font you would ever want to use installed. Unfortunately, we don’t live in a perfect world. @font-face aside, web designers are pretty much limited to the few so called web-safe fonts (e.g.

Arial, Georgia, serif, etc.). There is a plus side, though. You can still use fonts like Helvetica that aren’t necessarily installed on every computer.

The secret lies in font stacks. Font stacks are a way for developers to provide fallback fonts for the browser to display if the user doesn’t have the preferred font installed. For example:

#selector { font-family: Helvetica; }

Can be expanded with fallback fonts as such:

#selector { font-family: Helvetica, Arial, sans-serif; }

Now, if the user doesn’t have Helvetica, they can see your site in Arial, and if that doesn’t work, it’ll just default to any sans-serif font installed.

By defining fallback fonts, you gain more control as to how your web pages are rendered.

9. Unnecessary Whitespace

When it comes to trying to reduce your CSS file sizes for performance, every space counts. When you’re developing, it’s OK to format your code in the way that you’re comfortable with. However, there is absolutely no reason not to take out excess characters (a process known as minification) when you actually push your project onto the web where the size of your files really counts.

Too many developers simply don’t minify their files before launching their websites, and I think that’s a huge mistake. Although it may not feel like it makes much of a difference, when you have huge CSS files

10. Not Organizing Your CSS in a Logical Way

When you’re writing CSS, do yourself a favor and organize your code. Through comments, you can insure that the next time you come to make a change to a file you’ll still be able to navigate it. 

I personally like to organize my styles by how the HTML that I’m styling is structured. This means that I have comments that distinguish the header, body, sidebar, and footer. A common CSS-authoring mistake I see is people just writing up their styles as soon as they think of them.

The next time you try to change something and can’t find the style declaration, you’ll be silently cursing yourself for not organizing your CSS well enough.

11. Using Only One Stylesheet for Everything

This one’s subjective, so bear with me while I give you my perspective. I am of the belief, as are others, that it is better to split stylesheets into a few different ones for big sites for easier maintenance and for better modularity. Maybe I’ll have one for a CSS reset, one for IE-specific fixes, and so on.

By organizing CSS into disparate stylesheets, I’ll know immediately where to find a style I want to change. You can do this by importing all the stylesheets into a stylesheet like so:

@import url("reset.css"); @import url("ie.css"); @import url("typography.css"); @import url("layout.css"); 

Let me stress, however, that this is what works for me and many other developers. You may prefer to squeeze them all in one file, and that’s okay; there’s nothing wrong with that.

But if you’re having a hard time maintaining a single file, try splitting your CSS up.

12. Not Providing a Print Stylesheet

In order to style your site on pages that will be printed, all you have to do is utilize and include a print stylesheet. It’s as easy as:

<link rel="stylesheet" href="print.css" media="print" /> 

Using a stylesheet for print allows you to hide elements you don’t want printed (such as your navigation menu), reset the background color to white, provide alternative typography for paragraphs so that it’s better suited on a piece of paper, and so forth. The important thing is that you think about how your page will look when printed.

Too many people just don’t think about it, so their sites will simply print the same way you see them on the screen.


I Made These 2 BEGINNER CSS Mistakes

No matter how long you've been writing code, it's always a good time to revisit the basics. While working on a project the other day, I made 2 beginner mistakes with the CSS I was writing. I misunderstood both CSS specificity and how transform:scale affects the DOM!

Stack Overflow about transform:scale - https://stackoverflow.com/questions/32835144/css-transform-scale-does-not-change-dom-size 
CSS Specificity - https://www.w3schools.com/css/css_specificity.asp 

#css 

Alisha  Larkin

Alisha Larkin

1622879640

Top 10 Common CSS Mistakes Web Developers Make

The goal isn't to just write CSS that works. Code CSS that is efficient and easy to maintain

Here are some common mistakes that most web developers make, and how identifying and avoiding them can help you write better and more efficient CSS!

1. Using Color Names Instead of Hexadecimal

2. Hard Coding px Instead of Relative Units

3. Not Using Font Fallbacks

4. Not Using CSS Shorthands

5. Over Qualifying Selectors

6. Using ID’s instead of Classes

7. Not Using CSS Reset

8. Repetitive Code (Redundant Selectors and Properties)

9. Not Separating Design from Layout

10. Writing Unorganized CSS


1. Using Color Names Instead of Hexadecimal

When you say color: blue; you're essentially telling the computer to display whatever shade of color it thinks blue is. By doing this, you’re giving the browser control over how your web page should be displayed, and as a developer, this is something you should never do. By vaguely naming the color as blue, it can easily differ from the shade of blue that you had in mind, and worse it can also vary from browser to browser.

Using hexadecimal values eg. color: #4169E1; hence becomes something that all developers should adopt. It ensures specificity, is supported by all browsers, and gives back to you the control to decide exactly how you want your web page to be displayed.

Note: An efficient way for finding and using hexadecimal values is by first putting in the name of the closest color to the desired shade, and then inspecting the element to find the hex value using the color dropper.

2. Hard Coding px Instead of Relative Units

While it sometimes becomes imperative to use absolute px values, you should always use relative measurements such as em, % (percent), rem (root-Em), and others whenever possible.

This ensures that the website scales proportionally according to the user’s choice of zoom level and screen/browser size.

So, instead of declaring the sizes in absolutes,

p {
    font-size: 16px;
    line-height: 20px;
    margin-bottom: 8px;
}

do this instead:

p {
    font-size: 1rem;
    line-height: 1.25em;
    margin-bottom: 0.5rem;
}

3. Not Using Font Fallbacks

No matter how beautiful a particular font makes your page look, or how much it catches the eye, you always have to consider that not all font types are supported by all computers. If you’re using a font that some browsers do not support, it means that your web page might not be as beautiful and eye-catching as you’re designing it to be for all users.

So, after you use your favorite font, say Helvetica, always make sure to list fallback fonts that the browser can use in case it isn't supported.

Instead of writing this,

#selector {
  font-family: Helvetica;
}

expand the code by font fallbacks such as:

#selector {
  font-family: Helvetica, Arial, sans-serif;
}

Now, even if the browser doesn't support Helvetica, it would fall back to the second most preferred option Arial before going to the browser default.

4. Not Using CSS Shorthands

Take a look at the CSS below:

font-family: Helvetica;
font-size: 14px;
font-weight: bold;
line-height: 1.5;

This can easily be condensed into a single line using the CSS shorthand for font:

font: bold 14px/1.5 Helvetica;

Similarly, this list of properties for a background image:

background-image: url(background.png);
background-repeat: repeat-y;
background-position: center top;

can be written as:

background: url(background.png) repeat-y center top;

Why are we doing this? The reason is simple. It not only reduces your CSS file size but also makes the stylesheet easier to read and more organized.

Here’s a list of CSS shorthands to help you write cleaner code. It’s all about ingraining it into your coding habits, but once you do it, there's no going back!

5. Over Qualifying Selectors

Just like every other thing in excess, too much specificity is a bad thing. And more often than not, it is not even necessary. Take a look at the CSS below:

header #nav ul li a {...}

First of all, the header specification is absolutely unnecessary since an ID, having the highest specificity, has already been used (IDs are unique and only associated with one element). Moreover, an unordered list (ul) always has list items (li) within. So having to mention that becomes pointless. The same selector can now be written as:

#nav ul a {...}

With CSS there are only two levels of specificity — specific and not specific enough. Including all those extra elements might make it look ‘safe’ but are actually unnecessary and are only adding to the length of your stylesheet.

As a general rule, your selectors should be as short as possible. Be just as specific as is necessary for it to work.

6. Using ID’s instead of Classes

The most cogent argument against using ID’s is that it has a much higher specificity than classes, which is why it becomes hard to overwrite and extend your styles. A class on its own can’t overwrite styles belonging to an ID. To “beat” the ID, you would need either more IDs or to use !important, which can begin specificity wars in your stylesheets.

Class selectors can also be used for several HTML elements on the same page, unlike IDs which are unique to each element. Being able to reuse styles is one of the advantages of CSS.

To maintain a consistent convention, use only the class attributes to define styles and IDs while integrating interactivity with Javascript instead.

7. Not Using CSS Reset

If you have ever displayed an HTML page with no CSS styling, you know that the web browser itself “styles” the page using some default values as a fallback. The text has a particular font size and style, the margin and padding are set to certain values.

While this is a good thing for someone who does not use CSS, it is important to first reset these values when you put your own styles into the page. These values vary from browser to browser, hence a CSS Reset is the only way to ensure that all your styles are uniform and effective.

This entails resetting all the styles of all the HTML elements to a predictable baseline value. Once you do this, you can style all the elements on your page as if they were the same to start with. A blank slate.

An easier but incomplete way to do this is by resetting the margin and padding using a universal selector:

* {margin:0; padding:0;}

For a complete reset, however, you can use the Eric Meyer reset (modifying it as per your choice), to reset borders, underlines, and colors of elements like list items, links, and tables so that you don’t run into unexpected inconsistencies between web browsers.

8. Repetitive Code (Redundant Selectors and Properties)

In general, repeating yourself while coding is not considered a good practice. CSS is no different. Take a look at the code below:

#selector-1 {
  font-style: italic;
  color: #e7e7e7;
  margin: 5px;
  padding: 20px
}
.selector-2 {
  font-style: italic;
  color: #e7e7e7;
  margin: 5px;
  padding: 20px
}

A better way to write this is by combining them, with the selectors separated by a comma (,):

#selector-1, .selector-2 {
  font-style: italic;
  color: #e7e7e7;
  margin: 5px;
  padding: 20px
}

This is not just more efficient, but also reduces maintenance time and page-load speed.

9. Not Separating Design from Layout

The job of CSS is to provide styling, and the job of HTML is to provide structure. Generally, HTML should be written in a way that captures the information hierarchy of the page, ignoring any design concerns. Afterward, CSS can be added to make things ‘look nice.’

However, while HTML provides structure, it cannot always position elements on the exact spot of a page you want it to appear, which is where we use CSS to scaffold the layout of the page. Once an element is put into the right place on the page, it’s easy to style it without worrying about the display and position. This is why Layout CSS should be separated from Design CSS.

Instead of putting the layout as well as design properties together,

.article {
  display: inline-block;
  width: 50%;
  margin-bottom: 1em;
  font-family: sans-serif;
  border-radius: 1rem;
  box-shadow: 12px 12px 2px 1px rgba(0, 0, 0, .2);
}
.sidebar {
  width: 25%;
  margin-left: 5px;
}
<div class="article"></div>
<div class="article sidebar"></div>

Separate the design and layout of elements:

/* layout */
.article, .sidebar {
  display: inline-block;
}
.article {
  width: 50%;
  margin-bottom: 1em;
}
.sidebar {
  width: 25%;
  margin-left: 5px;
}

/* display */
.card {
  font-family: sans-serif;
  border-radius: 1rem;
  box-shadow: 12px 12px 2px 1px rgba(0, 0, 0, .2);
}
<div class="article card"></div>
<div class="sidebar card"></div>

This ensures separation of concerns, which is a common software engineering principle that helps keep our code maintainable and easy to understand.

10. Writing Unorganized CSS

Instead of writing your styles just as you think of them, do yourself a favor and organize your code neatly. This will ensure that next time you come to make a change to your file, you’re still able to navigate it.

  • Comment your CSS: A good tip is to add a block of comments between logical sections in your stylesheet too, to help locate different sections quickly when scanning through, or even give you something to search for to jump right into that part of the CSS. You don’t need to comment every single thing in your CSS, as much of it will be self-explanatory. What you should comment are the things where you made a particular decision for a reason.
  • Create Logical Sections in your Stylesheet: It is a good idea to have all of the common styling first in the stylesheet. This means all of the styles which will generally apply unless you do something special with that element. You will typically have rules set up for body, p, h1, h2, h3, links, and tables. After this, you can have a few utility classes, or properties, things you know you will want to apply to lots of different elements. Finally, include CSS for specific things, broken down by the context, page, or even component in which they are used.

#css #programming #developer 

Roscoe  Batz

Roscoe Batz

1662112200

SwiftIcons: Swift Library for Font Icons

Swift Library for Font Icons

Please ★ this library.

Now, you don't have to download different libraries to include different font icons. This SwiftIcons library helps you use icons from any of the following font icons.

  • Dripicons
  • Emoji
  • FontAwesome
  • Icofont
  • Ionicons
  • Linearicons
  • Map-icons
  • Material icons
  • Open iconic
  • State face icons
  • Weather icons
  • TypIcons

SwiftIcons supports different objects from the object library.

  • UIImage
  • UIImageView
  • UILabel
  • UIButton
  • UISegmentedControl
  • UITabBarItem
  • UISlider
  • UIBarButtonItem
  • UIViewController
  • UITextfield
  • UIStepper

Requirements

  • iOS 9.0+
  • Xcode 8

Installation

Cocoapods

CocoaPods is a dependency manager for Cocoa projects.

Make sure you have the latest version of CocoaPods by running:

$ gem install cocoapods
# (or if the above fails)
$ sudo gem install cocoapods

Update your local specs repo by running:

$ pod repo update

Add the following lines to your Podfile:

target 'YourProject' do
    use_frameworks!
    pod 'SwiftIcons', '~> 3.0'
end

Then run the following command

$ pod install

You can start using the library by importing it wherever you want

import SwiftIcons

Carthage

Carthage is a decentralized dependency manager for Cocoa projects.

Install the latest version of Carthage.

Add this line to your Cartfile:

github "ranesr/SwiftIcons" ~> 3.0

or for master,

github "ranesr/SwiftIcons" "master"

Then run carthage update --platform ios and add the built framework to your project by following these instructions from Carthage.

Manually

Copy all the files from Source folder. Link to files.

  • SwiftIcons.swift
  • Dripicons.ttf
  • Emoji.ttf
  • FontAwesomeBrands.ttf
  • FontAwesomeRegular.ttf
  • FontAwesomeSolid.ttf
  • Icofont.ttf
  • Ionicons.ttf
  • Linearicons.ttf
  • MapIcons.ttf
  • MaterialIcons.ttf
  • OpenIconic.ttf
  • Stateface.ttf
  • WeatherIcons.ttf
  • TypIcons.ttf

Check to import all ttf files in project, "Project" > "Target" > "Copy Bundle Resources"

Library Reference

You can check library reference documentation here.

Usage

  • No more image icons any more

There are different font types for each of the font icons

Font IconsVersionFont TypesIcons
Dripicons2.0dripiconsdripicons
Emoji emojiemoji
FontAwesome5.1.0fontAwesomefontAwesome
Icofont1.0.0 Betaicofonticofont
Ionicons2.0.1ioniconsionicons
Linearicons1.0.0linearIconslinearIcons
Map-icons3.0.2mapiconsmapicons
Material icons2.2.0googleMaterialDesigngoogleMaterialDesign
Open iconic1.1.1openIconicopenIconic
State face icons statestate
Weather icons2.0.10weatherweather
TypIcons2.0.7TypIconsTypicons

When setting an icon to any object, you have to mention which font type it is and then select which icon you want to set from that particular font icon.

UIImage

import SwiftIcons

UIImage.init(icon: .emoji(.airplane), size: CGSize(width: 35, height: 35))

// Icon with colors
UIImage.init(icon: .emoji(.airplane), size: CGSize(width: 35, height: 35), textColor: .red)
UIImage.init(icon: .emoji(.airplane), size: CGSize(width: 35, height: 35), textColor: .white, backgroundColor: .red)

// Stacked icons with bigger background
UIImage.init(bgIcon: .fontAwesomeRegular(.circle), topIcon: .fontAwesomeRegular(.square))

// Stacked icons with smaller background
UIImage.init(bgIcon: .fontAwesomeSolid(.camera), topIcon: .fontAwesomeSolid(.ban), topTextColor: .red, bgLarge: false)

// Stacked icons with custom size
UIImage.init(bgIcon: .fontAwesomeSolid(.camera), topIcon: .fontAwesomeSolid(.ban), topTextColor: .red, bgLarge: false, size: CGSize(width: 50, height: 50))

UIImageView

import SwiftIcons

// Setting icon to image view
imageView.setIcon(icon: .weather(.rainMix))

// Icon with colors
imageView.setIcon(icon: .mapicons(.amusementPark), textColor: .white, backgroundColor: .blue, size: nil)

UILabel

import SwiftIcons

// Setting icon to label
label.setIcon(icon: .ionicons(.paintbrush), iconSize: 70)

// Icon with colors
label.setIcon(icon: .googleMaterialDesign(.rowing), iconSize: 70, color: .white, bgColor: textColor)

// Icon with text around it
label.setIcon(prefixText: "Bus ", icon: .linearIcons(.bus), postfixText: " icon", size: 20)

// Icon with color & colored text around it
label.setIcon(prefixText: "Medal ", prefixTextColor: .red, icon: .ionicons(.ribbonA), iconColor: .red, postfixText: "", postfixTextColor: .red, size: nil, iconSize: 40)

// Icon with text with different fonts around it
label.setIcon(prefixText: "Font ", prefixTextFont: font1!, icon: .fontAwesomeSolid(.font), postfixText: " icon", postfixTextFont: font2!)

// Icon with text with different fonts & colors around it
label.setIcon(prefixText: "Bike ", prefixTextFont: font1!, prefixTextColor: .red, icon: .mapicons(.bicycling), iconColor: textColor, postfixText: " icon", postfixTextFont: font2!, postfixTextColor: .blue, iconSize: 30)

UIButton

import SwiftIcons

// Setting icon to button
button.setIcon(icon: .linearIcons(.phone), forState: .normal)

// Icon with size and color
button.setIcon(icon: .openIconic(.clipboard), iconSize: 70, color: .blue, forState: .normal)

// Icon with text around it
button.setIcon(prefixText: "Please ", icon: .googleMaterialDesign(.print), postfixText: " print", forState: .normal)

// Icon with color & colored text around it
button.setIcon(prefixText: "Lock ", prefixTextColor: .red, icon: .googleMaterialDesign(.lock), iconColor: .yellow, postfixText: " icon", postfixTextColor: .blue, forState: .normal, textSize: 15, iconSize: 20)

// Icon with text with different fonts around it
button.setIcon(prefixText: "Happy ", prefixTextFont: font1!, icon: .ionicons(.happy), postfixText: " face", postfixTextFont: font2!, forState: .normal)

// Icon with text with different fonts & colors around it
button.setIcon(prefixText: "Pulse ", prefixTextFont: font1!, prefixTextColor: .darkGray, icon: .openIconic(.pulse), iconColor: .red, postfixText: " icon", postfixTextFont: font2!, postfixTextColor: .purple, forState: .normal, iconSize: 40)

// Icon with title below icon
button.setIcon(icon: .emoji(.ferrisWheel), title: "Ferris Wheel", color: .red, forState: .normal)

// Icon with title below icon with different color & custom font
button.setIcon(icon: .weather(.rainMix), iconColor: .yellow, title: "RAIN MIX", titleColor: .red, font: font!, backgroundColor: .clear, borderSize: 1, borderColor: .green, forState: .normal)

UISegmentedControl

import SwiftIcons

// Setting icon at particular index
segmentedControl.setIcon(icon: .linearIcons(.thumbsUp), forSegmentAtIndex: 0)
segmentedControl.setIcon(icon: .linearIcons(.thumbsDown), forSegmentAtIndex: 1)

// Icons with sizes & colors
segmentedControl.setIcon(icon: .fontAwesomeSolid(.male), color: .red, iconSize: 50, forSegmentAtIndex: 0)
segmentedControl.setIcon(icon: .fontAwesomeSolid(.female), color: .purple, iconSize: 50, forSegmentAtIndex: 1)

UITabBarItem

import SwiftIcons

// Setting icon to tab bar item
tabBar.items?[0].setIcon(icon: .fontAwesomeSolid(.font), size: nil, textColor: .lightGray)

// Stacked icons for tab bar item
tabBar.items?[1].setIcon(bgIcon: .fontAwesomeRegular(.circle), bgTextColor: .lightGray, topIcon: .fontAwesomeSolid(.square), topTextColor: .lightGray, bgLarge: true, size: nil)

UISlider

import SwiftIcons

// Change minimum & maximum value icons
slider.setMaximumValueIcon(icon: .emoji(.digitNine))
slider.setMinimumValueIcon(icon: .emoji(.digitZero))

// Change minimum & maximum value icons with colors
slider.setMaximumValueIcon(icon: .linearIcons(.pointerUp), customSize: nil, textColor: .red, backgroundColor: .clear)
slider.setMinimumValueIcon(icon: .linearIcons(.pointerDown), customSize: nil, textColor: .blue, backgroundColor: .clear)

UIBarButtonItem

import SwiftIcons

// Setting icon to bar button item
barButtonItem.setIcon(icon: .ionicons(.iosFootball), iconSize: 30)

// Icon with colors
barButtonItem.setIcon(icon: .ionicons(.iosFootball), iconSize: 30, color: textColor)

// Icon with custom cgRect
barButtonItem.setIcon(icon: .ionicons(.iosFootball), iconSize: 30, color: textColor, cgRect: CGRect(x: 0, y: 0, width: 30, height: 30), target: self, action: #selector(barButtonItem(sender:)))

// Icon with text around it
barButtonItem.setIcon(prefixText: "Please ", icon: .ionicons(.iosDownload), postfixText: " download", cgRect: CGRect(x: 0, y: 0, width: 30, height: 30), size: 23, target: self, action: #selector(barButtonItem(sender:)))

// Icon with color & colored text around it
barButtonItem.setIcon(prefixText: "Blue ", prefixTextColor: .red, icon: .ionicons(.iosFootball), iconColor: .blue, postfixText: " football", postfixTextColor: .green, cgRect: CGRect(x: 0, y: 0, width: 30, height: 30), size: 20, iconSize: 30, target: self, action: #selector(barButtonItem(sender:)))

// Icon with text with different fonts around it
barButtonItem.setIcon(prefixText: "Digit ", prefixTextFont: font1!, icon: .emoji(.digitOne), postfixText: " One", postfixTextFont: font2!, cgRect: CGRect(x: 0, y: 0, width: 30, height: 30), target: self, action: #selector(barButtonItem(sender:)))

// Icon with text with different fonts & colors around it
barButtonItem.setIcon(prefixText: "", prefixTextFont: font1!, prefixTextColor: .red, icon: .ionicons(.iosFootball), iconColor: .blue, postfixText: " football", postfixTextFont: font2!, postfixTextColor: .green, cgRect: CGRect(x: 0, y: 0, width: 30, height: 30), iconSize: 24, target: self, action: #selector(barButtonItem(sender:)))

UIViewController

import SwiftIcons

// Setting icon to the title
self.setTitleIcon(icon: .emoji(.animalHorse), iconSize: 30, color: .red)

UITextfield

import SwiftIcons

// Setting left view icon
textfield.setLeftViewIcon(icon: .fontAwesomeSolid(.search))

// Left view icon with colors & leftViewMode
textfield.setLeftViewIcon(icon: .state(.TX), leftViewMode: .always, textColor: .blue, backgroundColor: .clear, size: nil)
textfield.setLeftViewIcon(icon: .googleMaterialDesign(.plusOne), leftViewMode: .unlessEditing, textColor: .green, backgroundColor: .clear, size: nil)

// Setting right view icon
textfield.setRightViewIcon(icon: .openIconic(.questionMark))

// Right view icon with colors & rightViewMode
textfield.setRightViewIcon(icon: .weather(.rainMix), rightViewMode: .always, textColor: .red, backgroundColor: .clear, size: nil)

UIStepper

import SwiftIcons

// Setting icons
stepper.setDecrementIcon(icon: .ionicons(.iosPause), forState: .normal)
stepper.setIncrementIcon(icon: .ionicons(.iosPlay), forState: .normal)

Examples

Please check out the SwiftIcons App. In the demo project, if you click on any object, you will see the method description in the logs for the icon of that object.

SwiftIcons Mentions & Usage

If you are using SwiftIcons in your app and want to be listed here, simply create a new issue here.

I am always curious who is using my projects 😊

Author

Saurabh Rane

Special thanks to Patrik Vaberer and his initial work on Font-Awesome-Swift library


Download Details:

Author: ranesr
Source code: https://github.com/ranesr/SwiftIcons

License: MIT license
#swift 

Rupert  Beatty

Rupert Beatty

1675107840

SwiftIcons: Swift Library for Font Icons

SwiftIcons

Swift Library for Font Icons

Please ★ this library.

Now, you don't have to download different libraries to include different font icons. This SwiftIcons library helps you use icons from any of the following font icons.

  • Dripicons
  • Emoji
  • FontAwesome
  • Icofont
  • Ionicons
  • Linearicons
  • Map-icons
  • Material icons
  • Open iconic
  • State face icons
  • Weather icons
  • TypIcons

SwiftIcons supports different objects from the object library.

  • UIImage
  • UIImageView
  • UILabel
  • UIButton
  • UISegmentedControl
  • UITabBarItem
  • UISlider
  • UIBarButtonItem
  • UIViewController
  • UITextfield
  • UIStepper

Requirements

  • iOS 9.0+
  • Xcode 8

Installation

Cocoapods

CocoaPods is a dependency manager for Cocoa projects.

Make sure you have the latest version of CocoaPods by running:

$ gem install cocoapods
# (or if the above fails)
$ sudo gem install cocoapods

Update your local specs repo by running:

$ pod repo update

Add the following lines to your Podfile:

target 'YourProject' do
    use_frameworks!
    pod 'SwiftIcons', '~> 3.0'
end

Then run the following command

$ pod install

You can start using the library by importing it wherever you want

import SwiftIcons

Carthage

Carthage is a decentralized dependency manager for Cocoa projects.

Install the latest version of Carthage.

Add this line to your Cartfile:

github "ranesr/SwiftIcons" ~> 3.0

or for master,

github "ranesr/SwiftIcons" "master"

Then run carthage update --platform ios and add the built framework to your project by following these instructions from Carthage.

Manually

Copy all the files from Source folder. Link to files.

  • SwiftIcons.swift
  • Dripicons.ttf
  • Emoji.ttf
  • FontAwesomeBrands.ttf
  • FontAwesomeRegular.ttf
  • FontAwesomeSolid.ttf
  • Icofont.ttf
  • Ionicons.ttf
  • Linearicons.ttf
  • MapIcons.ttf
  • MaterialIcons.ttf
  • OpenIconic.ttf
  • Stateface.ttf
  • WeatherIcons.ttf
  • TypIcons.ttf

Check to import all ttf files in project, "Project" > "Target" > "Copy Bundle Resources"

Library Reference

You can check library reference documentation here.

Usage

  • No more image icons any more

There are different font types for each of the font icons

Font IconsVersionFont TypesIcons
Dripicons2.0dripiconsdripicons
Emoji emojiemoji
FontAwesome5.1.0fontAwesomefontAwesome
Icofont1.0.0 Betaicofonticofont
Ionicons2.0.1ioniconsionicons
Linearicons1.0.0linearIconslinearIcons
Map-icons3.0.2mapiconsmapicons
Material icons2.2.0googleMaterialDesigngoogleMaterialDesign
Open iconic1.1.1openIconicopenIconic
State face icons statestate
Weather icons2.0.10weatherweather
TypIcons2.0.7TypIconsTypicons

When setting an icon to any object, you have to mention which font type it is and then select which icon you want to set from that particular font icon.

UIImage

import SwiftIcons

UIImage.init(icon: .emoji(.airplane), size: CGSize(width: 35, height: 35))

// Icon with colors
UIImage.init(icon: .emoji(.airplane), size: CGSize(width: 35, height: 35), textColor: .red)
UIImage.init(icon: .emoji(.airplane), size: CGSize(width: 35, height: 35), textColor: .white, backgroundColor: .red)

// Stacked icons with bigger background
UIImage.init(bgIcon: .fontAwesomeRegular(.circle), topIcon: .fontAwesomeRegular(.square))

// Stacked icons with smaller background
UIImage.init(bgIcon: .fontAwesomeSolid(.camera), topIcon: .fontAwesomeSolid(.ban), topTextColor: .red, bgLarge: false)

// Stacked icons with custom size
UIImage.init(bgIcon: .fontAwesomeSolid(.camera), topIcon: .fontAwesomeSolid(.ban), topTextColor: .red, bgLarge: false, size: CGSize(width: 50, height: 50))

UIImageView

import SwiftIcons

// Setting icon to image view
imageView.setIcon(icon: .weather(.rainMix))

// Icon with colors
imageView.setIcon(icon: .mapicons(.amusementPark), textColor: .white, backgroundColor: .blue, size: nil)

UILabel

import SwiftIcons

// Setting icon to label
label.setIcon(icon: .ionicons(.paintbrush), iconSize: 70)

// Icon with colors
label.setIcon(icon: .googleMaterialDesign(.rowing), iconSize: 70, color: .white, bgColor: textColor)

// Icon with text around it
label.setIcon(prefixText: "Bus ", icon: .linearIcons(.bus), postfixText: " icon", size: 20)

// Icon with color & colored text around it
label.setIcon(prefixText: "Medal ", prefixTextColor: .red, icon: .ionicons(.ribbonA), iconColor: .red, postfixText: "", postfixTextColor: .red, size: nil, iconSize: 40)

// Icon with text with different fonts around it
label.setIcon(prefixText: "Font ", prefixTextFont: font1!, icon: .fontAwesomeSolid(.font), postfixText: " icon", postfixTextFont: font2!)

// Icon with text with different fonts & colors around it
label.setIcon(prefixText: "Bike ", prefixTextFont: font1!, prefixTextColor: .red, icon: .mapicons(.bicycling), iconColor: textColor, postfixText: " icon", postfixTextFont: font2!, postfixTextColor: .blue, iconSize: 30)

UIButton

import SwiftIcons

// Setting icon to button
button.setIcon(icon: .linearIcons(.phone), forState: .normal)

// Icon with size and color
button.setIcon(icon: .openIconic(.clipboard), iconSize: 70, color: .blue, forState: .normal)

// Icon with text around it
button.setIcon(prefixText: "Please ", icon: .googleMaterialDesign(.print), postfixText: " print", forState: .normal)

// Icon with color & colored text around it
button.setIcon(prefixText: "Lock ", prefixTextColor: .red, icon: .googleMaterialDesign(.lock), iconColor: .yellow, postfixText: " icon", postfixTextColor: .blue, forState: .normal, textSize: 15, iconSize: 20)

// Icon with text with different fonts around it
button.setIcon(prefixText: "Happy ", prefixTextFont: font1!, icon: .ionicons(.happy), postfixText: " face", postfixTextFont: font2!, forState: .normal)

// Icon with text with different fonts & colors around it
button.setIcon(prefixText: "Pulse ", prefixTextFont: font1!, prefixTextColor: .darkGray, icon: .openIconic(.pulse), iconColor: .red, postfixText: " icon", postfixTextFont: font2!, postfixTextColor: .purple, forState: .normal, iconSize: 40)

// Icon with title below icon
button.setIcon(icon: .emoji(.ferrisWheel), title: "Ferris Wheel", color: .red, forState: .normal)

// Icon with title below icon with different color & custom font
button.setIcon(icon: .weather(.rainMix), iconColor: .yellow, title: "RAIN MIX", titleColor: .red, font: font!, backgroundColor: .clear, borderSize: 1, borderColor: .green, forState: .normal)

UISegmentedControl

import SwiftIcons

// Setting icon at particular index
segmentedControl.setIcon(icon: .linearIcons(.thumbsUp), forSegmentAtIndex: 0)
segmentedControl.setIcon(icon: .linearIcons(.thumbsDown), forSegmentAtIndex: 1)

// Icons with sizes & colors
segmentedControl.setIcon(icon: .fontAwesomeSolid(.male), color: .red, iconSize: 50, forSegmentAtIndex: 0)
segmentedControl.setIcon(icon: .fontAwesomeSolid(.female), color: .purple, iconSize: 50, forSegmentAtIndex: 1)

UITabBarItem

import SwiftIcons

// Setting icon to tab bar item
tabBar.items?[0].setIcon(icon: .fontAwesomeSolid(.font), size: nil, textColor: .lightGray)

// Stacked icons for tab bar item
tabBar.items?[1].setIcon(bgIcon: .fontAwesomeRegular(.circle), bgTextColor: .lightGray, topIcon: .fontAwesomeSolid(.square), topTextColor: .lightGray, bgLarge: true, size: nil)

UISlider

import SwiftIcons

// Change minimum & maximum value icons
slider.setMaximumValueIcon(icon: .emoji(.digitNine))
slider.setMinimumValueIcon(icon: .emoji(.digitZero))

// Change minimum & maximum value icons with colors
slider.setMaximumValueIcon(icon: .linearIcons(.pointerUp), customSize: nil, textColor: .red, backgroundColor: .clear)
slider.setMinimumValueIcon(icon: .linearIcons(.pointerDown), customSize: nil, textColor: .blue, backgroundColor: .clear)

UIBarButtonItem

import SwiftIcons

// Setting icon to bar button item
barButtonItem.setIcon(icon: .ionicons(.iosFootball), iconSize: 30)

// Icon with colors
barButtonItem.setIcon(icon: .ionicons(.iosFootball), iconSize: 30, color: textColor)

// Icon with custom cgRect
barButtonItem.setIcon(icon: .ionicons(.iosFootball), iconSize: 30, color: textColor, cgRect: CGRect(x: 0, y: 0, width: 30, height: 30), target: self, action: #selector(barButtonItem(sender:)))

// Icon with text around it
barButtonItem.setIcon(prefixText: "Please ", icon: .ionicons(.iosDownload), postfixText: " download", cgRect: CGRect(x: 0, y: 0, width: 30, height: 30), size: 23, target: self, action: #selector(barButtonItem(sender:)))

// Icon with color & colored text around it
barButtonItem.setIcon(prefixText: "Blue ", prefixTextColor: .red, icon: .ionicons(.iosFootball), iconColor: .blue, postfixText: " football", postfixTextColor: .green, cgRect: CGRect(x: 0, y: 0, width: 30, height: 30), size: 20, iconSize: 30, target: self, action: #selector(barButtonItem(sender:)))

// Icon with text with different fonts around it
barButtonItem.setIcon(prefixText: "Digit ", prefixTextFont: font1!, icon: .emoji(.digitOne), postfixText: " One", postfixTextFont: font2!, cgRect: CGRect(x: 0, y: 0, width: 30, height: 30), target: self, action: #selector(barButtonItem(sender:)))

// Icon with text with different fonts & colors around it
barButtonItem.setIcon(prefixText: "", prefixTextFont: font1!, prefixTextColor: .red, icon: .ionicons(.iosFootball), iconColor: .blue, postfixText: " football", postfixTextFont: font2!, postfixTextColor: .green, cgRect: CGRect(x: 0, y: 0, width: 30, height: 30), iconSize: 24, target: self, action: #selector(barButtonItem(sender:)))

UIViewController

import SwiftIcons

// Setting icon to the title
self.setTitleIcon(icon: .emoji(.animalHorse), iconSize: 30, color: .red)

UITextfield

import SwiftIcons

// Setting left view icon
textfield.setLeftViewIcon(icon: .fontAwesomeSolid(.search))

// Left view icon with colors & leftViewMode
textfield.setLeftViewIcon(icon: .state(.TX), leftViewMode: .always, textColor: .blue, backgroundColor: .clear, size: nil)
textfield.setLeftViewIcon(icon: .googleMaterialDesign(.plusOne), leftViewMode: .unlessEditing, textColor: .green, backgroundColor: .clear, size: nil)

// Setting right view icon
textfield.setRightViewIcon(icon: .openIconic(.questionMark))

// Right view icon with colors & rightViewMode
textfield.setRightViewIcon(icon: .weather(.rainMix), rightViewMode: .always, textColor: .red, backgroundColor: .clear, size: nil)

UIStepper

import SwiftIcons

// Setting icons
stepper.setDecrementIcon(icon: .ionicons(.iosPause), forState: .normal)
stepper.setIncrementIcon(icon: .ionicons(.iosPlay), forState: .normal)

Examples

Please check out the SwiftIcons App. In the demo project, if you click on any object, you will see the method description in the logs for the icon of that object.

SwiftIcons Mentions & Usage

If you are using SwiftIcons in your app and want to be listed here, simply create a new issue here.

I am always curious who is using my projects 😊

Download Details:

Author: Ranesr
Source Code: https://github.com/ranesr/SwiftIcons 
License: MIT license

#swift #ios #fonts #ionic 

Hire CSS Developer

Want to develop a website or re-design using CSS Development?

We build a website and we implemented CSS successfully if you are planning to Hire CSS Developer from HourlyDeveloper.io, We can fill your Page with creative colors and attractive Designs. We provide services in Web Designing, Website Redesigning and etc.

For more details…!!
Consult with our experts:- https://bit.ly/3hUdppS

#hire css developer #css development company #css development services #css development #css developer #css