• Stars
    star
    143
  • Rank 257,007 (Top 6 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created over 10 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Create easy radio button groups in objective-c

Radio buttons for Objective-C

U can use the TNRadioButtonGroup class to use some pre-built radio buttons, or you can extend it really easy to suit your own design. Every radio button has a selected animation. When the text is longer then the available space, it will automatically wrap the text over multiple lines.

The current version supports 3 different radio buttons:

  1. Circle radio button

  2. Square radio button

  3. Custom image radio button

Screenshot

Installation

##Manual

  • Just drag the files in the src folder to your project.
  • Import the radio button class you want to use

##Cocoapods

  • Add pod 'TNRadioButtonGroup' to your Podfile
  • Done

How to use

Import the TNRadioButtonGroup.h file.

Create data objects for every option in your radio button group. You can use the following data objects: TNCircularRadioButtonData, TNRectangularRadioButtonData, TNImageRadioButtonData.

Every data object extends TNRadioButtonData, but has specific properties you can set.

You can set the following properties on every data object:

Property What does it do
identifier Set a human readeble name for the radio button
labelText Set the text for the label
selected Set the selected state
labelFont Set the font for the label
labelColor Set the color for the label

The different classes have the following extra properties:

TNCircularRadioButtonData

Arguments What does it do
borderColor Set the border color for the outer circle
circleColor Set the color for the inner circle
borderRadius Set the radius for the outer circle
circleRadius Set the color for the inner circle

Example

TNCircularRadioButtonData *maleData = [TNCircularRadioButtonData new];
maleData.labelText = @"Male";
maleData.identifier = @"male";
maleData.selected = YES;
maleData.borderColor = [UIColor blackColor];
maleData.circleColor = [UIColor blackColor];
maleData.borderRadius = 12;
maleData.circleRadius = 5;

TNRectangularRadioButtonData

Arguments What does it do
borderColor Set the border color for the outer rectangle
rectangleColor Set the color for the inner rectangle
borderWidth Set the width for the outer rectangle
borderHeight Set the height for the outer rectangle
rectangleWidth Set the width for the inner rectangle
rectangleHeight Set the height for the inner rectangle

Example

TNRectangularRadioButtonData *snowboardData = [TNRectangularRadioButtonData new];
snowboardData.labelText = @"Snowboarding";
snowboardData.identifier = @"snowboarding";
snowboardData.selected = NO;
snowboardData.borderColor = [UIColor blackColor];
snowboardData.rectangleColor = [UIColor blackColor];
snowboardData.borderWidth = snowboardData.borderHeight = 12;
snowboardData.rectangleWidth = snowboardData.rectangleHeight = 5;

TNImageRadioButtonData

Arguments What does it do
selectedImage Set the image for the selected state
unselectedImage Set the image for the unselected state

Example

TNImageRadioButtonData *coldData = [TNImageRadioButtonData new];
coldData.labelText = @"Hot";
coldData.identifier = @"hot";
coldData.selected = YES;
coldData.unselectedImage = [UIImage imageNamed:@"unchecked"];
coldData.selectedImage = [UIImage imageNamed:@"checked"];

When you have created the different data objects, you can create an instance of TNRadioButtonGroup and pass an array with the data objects and set the layout. The layout can be horizontal or vertical.

You can also set the margin between each item via the marginBetweenItems property. By default it's set to 15 pixels.

If you want, you can also set the identifier property to have a human readable name for the group.

Call the create method and set the position property to place the group at a specific point. Don't worry about the frame, this will be automatically calculated. And add the object to the view hierarchy.

self.myGroup = [[TNRadioButtonGroup alloc] initWithRadioButtonData:@[data1, data2, data3] style:TNRadioButtonGroupStyleHorizontal];
self.myGroup.identifier = @"My group";
[self.myGroup create];
self.myGroup.position = CGPointMake(15, 50);
[self addSubview:self.myGroup];

Done!

##Getting change notifications

When the state of the radio button group has changed an NSNotification is broadcasted.

You can listen to the SELECTED_RADIO_BUTTON_CHANGED message.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myGroupGotUpdated:) name:SELECTED_RADIO_BUTTON_CHANGED object:self.myGroup];

Styling checkboxes

Group-level

If you want to provide the same label font and color for all radio buttons in a specific group, you can set the labelFont and labelColor properties.

Checkbox-level

If you want to provide different label fonts and/or colors for specific radio buttons in a group, you can set the labelFont and labelColor properties on the TNRadioButton subclass.

Creating custom radio buttons

If you want it is really easy to add your own custom radio buttons.

  1. Create a data model class in which you store the specific data for your checkbox. This class needs to extend TNRadioButtonData.

  2. Create a radio button class which extends TNRadioButton.

  3. Override the setup method (don't forget to call the super method at the end).

  4. Override the createRadioButton method, here you can draw your custom radio button.

  5. Override the checkWithAnimation: method to supply your own animations when the radio button state changes.

  6. There is no step 6.

Demo

There is a demo project added to this repository, so you can see how it works.

License

TNRadioButtonGroup published under the MIT license:

Copyright (C) 2014, Frederik Jacques

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

sketch-favicon-exporter-template

Easily generate favicons for today's devices with Sketch.
219
star
2

TNCheckBoxGroup

Customisable checkbox class for Objective-C
Objective-C
183
star
3

TNSexyImageUploadProgress

An easy image upload visualiser for Objective-C
Objective-C
143
star
4

TNImageSliderViewController

A Swift image slider component based on UICollectionView
Swift
90
star
5

native-ios-and-unity3d

Objective-C
56
star
6

DynamicColladaSceneKitPrototype

You can find more info about this prototype at http://www.the-nerd.be/2014/11/07/dynamically-load-collada-files-in-scenekit-at-runtime/
Objective-C
44
star
7

3d-touch-peek-and-pop-tutorial

This tutorial is part of a blog post
Swift
31
star
8

touchid-swift-tutorial

The project associated to the blog post on how to integrate TouchID.
Swift
26
star
9

objc-login-system

An easy way to create a login system in obj-c
Objective-C
22
star
10

TNSwiftyCheckboxGroup

A Swift component based on UICollectionView to create checkbox groups with various styles.
Swift
21
star
11

quick-actions-3d-touch-tutorial

Swift
19
star
12

TNColorScrollViewController

A view controller for Objective-C which creates a scrollview with view with different background colors.
Objective-C
15
star
13

TNInfoBubble

Prisma-style info bubbles for your iOS application
Swift
15
star
14

countrykit

Swift library with all continents, regions, subregions & countries of the world according to Standard Country or Area Codes for Statistical Use (UN M94).
Swift
12
star
15

vatnumberkit

VatNumberKit - A Swift VAT validation library for iOS & macOS
Swift
11
star
16

FaceRecognitionTrainer

iPhone prototype app for Face.com
Objective-C
9
star
17

TNGradientPickerSlider

TNGradientPickerSlider is a ready-to-use AppKit component which lets your users select multiple colors to build a gradient for your macOS application.
Swift
9
star
18

ios-pongwars

A Swift implementation of Pong Wars with SpriteKit.
Swift
6
star
19

messaging-objc-unity

Objective-C++
5
star
20

ZDepthAnimation

Mimic the National Geographic - National Parcs iPad app animation
Objective-C
4
star
21

uiscrollview-extensions

For the lazy ones who hate to divide offset.x by bounds.width
Shell
4
star
22

LightWeightCoreDataMigration

How to do a leightweight core data migration
Objective-C
2
star
23

UIFont-Enumerate

Swift extension to enumerate all available font families with their font names on the device
Ruby
2
star
24

UIColor-Hex-Swift

Create UIColor objects in Swift from a CSS color string or a hexadecimal integer.
Shell
1
star
25

cocoaheads-rxswift-primer

The demo project for my CocoaHeads presentation on November 22, 2017.
Swift
1
star
26

DictaPoint

A prototype mac app to grade students via voice recognition
C
1
star