Giter Club home page Giter Club logo

bylbadgeview's Introduction

BYLBadgeView Build Status

A badge view using TextKit APIs for rendering the badge that supports animations and UIAppearance.

Requirements

BYLBadgeView requires iOS7+.

Installation

Using cocoapods:

pod 'BYLBadgeView', :git => 'https://github.com/scondoo/BYLBadgeView.git'

To use without cocoapods copy the files under the Sources directory into your project.

Usage

To use instantate the view and add it as a subview to the view you'd like to be placed within. The badge view calculates an intrinsict content size, and I recommend just adding enough constraints to position it where you'd like. Here's how to add it to a button and place it in the top-right corner of the button:

BYLBadgeView *badgeView = [[BYLBadgeView alloc] initWithBadge:@"20"];
badgeView.translatesAutoresizingMaskIntoConstraints = NO;
badgeView.opaque = NO;
badgeView.layer.zPosition = 10; // UIButton subviews are added later so we need to ensure this one is in front
[self.badgedButton addSubview:badgeView]; // Created from IB
[self.badgedButton addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[badgeView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(badgeView)]];
[self.badgedButton addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[badgeView]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(badgeView)]];

Setting Badge Text Color

You can set the badge text color with both the badgeTextColor property or by setting the NSForegroundColorAttributeName key in the badgeTextAttributes. The badgeTextColor property exists to allow implicit animations. More on animations below.

UIAppearance

The BYLBadgeView supports the UIAppearance proxy for badgeRadius, badgeTextAttributes, badgeBackgroundColor, and badgeTextColor. Here's an example applying style to all badges contained within a UIButton:

[[BYLBadgeView appearanceWhenContainedIn:[UIButton class], nil] setBadgeBackgroundColor:[UIColor redColor]];
[[BYLBadgeView appearanceWhenContainedIn:[UIButton class], nil] setBadgeTextColor:[UIColor purpleColor]];
[[BYLBadgeView appearanceWhenContainedIn:[UIButton class], nil] setBadgeRadius:10.0f];
NSDictionary *titleAttributes = @{NSFontAttributeName: [UIFont fontWithDescriptor:[UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleSubheadline] size:8.0f]};
[[BYLBadgeView appearanceWhenContainedIn:[UIButton class], nil] setBadgeTextAttributes:titleAttributes];

Animations

The BYLBadgeView supports implicit animations on the badgeTextColor, badgeBackgroundColor, and badgeRadius properties. Here's an example:

self.animatingBadgeView.badge = @"500"; // self.animatingBadgeView was previously instantated
[UIView animateWithDuration:5.0f delay:0.0f options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionRepeat animations:^{
  self.animatingBadgeView.badgeTextColor = [UIColor blueColor];
  self.animatingBadgeView.badgeBackgroundColor = [UIColor orangeColor];
} completion:nil];

Caveats

-[NSString drawInRect:withAttributes:] behaves oddly with the NSBackgroundColorAttributeName key set on the textAttributes dictionary. Because of that, if that key exists in the badgeTextAttributes dictionary it will be removed when set.

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.