Git Product home page Git Product logo

swift-badge's Introduction

Swift Badge

A badge control for iOS and tvOS written in Swift

Carthage compatible CocoaPods Version License Platform

  • The badge is a subclass of UILabel view.
  • It can be created and customized from the Storyboard or from the code.

Setup

There are various ways you can add BadgeSwift to your Xcode project.

Add source (iOS 7+)

Simply add BadgeSwift.swift file to your project.

Setup with Carthage (iOS 8+)

Alternatively, add github "evgenyneu/swift-badge" ~> 8.0 to your Cartfile and run carthage update.

Setup with CocoaPods (iOS 8+)

If you are using CocoaPods add this text to your Podfile and run pod install.

use_frameworks!
target 'Your target name'
pod 'BadgeSwift', '~> 8.0'

Setup with Swift Package Manager

Legacy Swift versions

Setup a previous version of the library if you use an older version of Swift.

Usage

Creating a badge in the Storyboard

  • Drag a Label to your view.
  • Set its class to BadgeSwift in identity inspector.
  • Set the module property to BadgeSwift (unless you used the file setup method).

Add badge to storyboard

  • Customize the badge properties in the attributes inspector (text, color and other).
  • If storyboard does not show the badge correctly click Refresh All Views from the Editor menu.

Change badge properties in attribute inspector

Note: Carthage setup method does not allow to customize Cosmos view from the storyboard, please do it from code instead.

Creating a badge from the code

Add import BadgeSwift to your source code unless you used the file setup method.

let badge = BadgeSwift()
view.addSubview(badge)
// Position the badge ...

See example of how to create and position the badge from code in the demo app.

Customization

// Text
badge.text = "2"

// Insets
badge.insets = CGSize(width: 12, height: 12)

// Font
badge.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle.body)

// Text color
badge.textColor = UIColor.yellow

// Badge color
badge.badgeColor = UIColor.black

// Shadow
badge.shadowOpacityBadge = 0.5
badge.shadowOffsetBadge = CGSize(width: 0, height: 0)
badge.shadowRadiusBadge = 1.0
badge.shadowColorBadge = UIColor.black

// No shadow
badge.shadowOpacityBadge = 0

// Border width and color
badge.borderWidth = 5.0
badge.borderColor = UIColor.magenta

// Customize the badge corner radius.
// -1 if unspecified. When unspecified, the corner is fully rounded. Default: -1.
badge.cornerRadius = 10

Demo app

This project includes a demo app.

BadgeSwift for iOS demo app

Show BadgeSwift in a table view in iOS

Create badge from code BadgeSwift for iOS

Alternative solutions

Here are some alternative badges for iOS.

Thanks to 👍

  • amg1976 for adding a border and redesigning the drawing.
  • gperdomor for adding ability to customize corner radius.
  • acecilia for removing a warning when the badge is used in an extension.

License

BadgeSwift is released under the MIT License.

Feedback is welcome

If you found a bug or want to improve the badge feel free to create an issue.

swift-badge's People

Contributors

acecilia avatar amg1976 avatar evgenyneu avatar gperdomor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

swift-badge's Issues

I can't import BadgeSwift

when in try to use swift-badge I can't import BadgeSwift
I installed pod but I can't import it

  • Xcode version : 9.0.1.

error line

translatesAutoresizingMaskIntoConstraints = false

Render Issue on Storyboad

I have added Bade on story and then, it start complaining some random error.

Failed to render and update auto layout status for [My View Controller Name].swift "
Required code signature missing for BadeSwift.framework missing. 
  • Library setup method: file, Carthage, CocoaPods or Swift Package Manager.
  • Version of the library : 5.0.1
  • Xcode version. Example: 9.3
  • OS version. Example: iOS 11

The small red part

Please consider submitting the following information (if relevant):

  • Library setup method: CocoaPods.
  • Version of the library. Example: 5.0.1.
  • Xcode version. Example: 9.4.1.
  • OS version. Example: iOS 11.4.

The small red part that appears to be cut off on the top right always pops up. What should I do in order to remove it?

img_2880

Thank you!

.Hidden

The .hidden property on the badge seems to hide only the red background. The white text still remains visible. I have tried setting it to "" but it remains the previous value. Any ideas? Thanks

if listingsToLoad[indexPath.row].unReadMessages.value != nil {
    cell.cellBadge.hidden = false
    cell.cellBadge.text = String(listingsToLoad[indexPath.row].unReadMessages.value!)
} else {
    cell.cellBadge.hidden = true
    cell.cellBadge.text = ""
}

Swift 5 support

Xcode 10.2 Beta now comes with Swift 5. Any plans on updating swift-badge to support it? Thanks!

[Bug] Top left stroke edge error

The corner radius support will be added in #10, but if the radius is 0, and the label has border, the top left border has an error

screen shot 2016-11-09 at 10 49 37 am

Without border:
screen shot 2016-11-09 at 10 50 56 am

Swift 4.2

Hi,

I'm using the last version of the library from cocoapods (BadgeSwift (5.0.1)) and it doesn't build on Swift4.2.
Is there any plan to update it?
It's just one line of code actually:
let rectWithoutInsets = UIEdgeInsetsInsetRect(rect, insets)
to
let rectWithoutInsets = rect.inset(by: insets)

Thanks,

Why not add it to cocospod?

This is very useful for me. And I think it's much more flexible than other badges you have listed.Why not add it to cocospod?

ld: warning: linking against a dylib which is not safe for use in application extensions

As explained here, there are some APIs unavailable for extensions. Seems like BadgeSwift is using them, because I am getting the error:

ld: warning: linking against a dylib which is not safe for use in application extensions

Any idea which is the part of the code that is using this kind of APIs? Looking at the functionality of this framework, it should be possible to use it in extensions.

Various compile errors with example

swift-badge-master/swift-badge/SwiftBadge.swift:15:12: Initializer does not override a designated initializer from its superclass

swift-badge-master/swift-badge/SwiftBadge.swift:16:5: Must call a designated initializer of the superclass 'UILabel'

swift-badge-master/swift-badge/ViewController.swift:50:12: 'NSString' is not implicitly convertible to 'String'; did you mean to use 'as' to explicitly convert?

I' m using latest Xcode and swift.

What version is in Swift 2.3?

I am having trouble finding what Version supports swift 2.3?

I have gone as fat back as 2.011. Could you include this in the docs for future reference?

Thanks!

different shape and arrow

it's possible to add and arrow like disclosure indicator to the badge? And it's possible have different shape?

Gradient

Is there a way to specify a gradient for the badge color?

Subclassing when using CocoaPods

Hi!

I wanted to subclass BadgeSwift to add some custom default styling. But when i tried i got..

Cannot inherit from non-open class 'BadgeSwift' outside of it´s defining module.

I´m using CocoaPods. I guess this is just because of the missing open declaration.
Could we add this? Or is the class intentionally non-open?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.