<commit_before>//
//  ISRadioButton.swift
//  DaynamicForm
//
//  Created by <NAME> on 09/12/2015.
//  Copyright © 2015 TheGoal. All rights reserved.
//

import UIKit

@IBDesignable
public class  ISRadioButton: UIButton {
    
    var indexPath:NSIndexPath!
    
    //      Container for holding other buttons in same group.
    
    @IBOutlet var otherButtons: Array<ISRadioButton>?
    
    //      Size of icon, default is 15.0.
    
    @IBInspectable public var iconSize:CGFloat = 15.0
    
    //    Size of selection indicator, default is iconSize * 0.5.
    
    @IBInspectable public var indicatorSize:CGFloat = 15.0 * 0.5
    
    //      Color of icon, default is black
    
    @IBInspectable public var iconColor:UIColor =  UIColor.blackColor()
    
    //      Stroke width of icon, default is iconSize / 9.
    
    @IBInspectable public var iconStrokeWidth:CGFloat = 1.6
    
    //      Color of selection indicator, default is black
    
    @IBInspectable public var indicatorColor:UIColor = UIColor.blackColor()
    
    //      Margin width between icon and title, default is 10. 0.
    
    @IBInspectable public var marginWidth:CGFloat = 10.0
    
    //      Whether icon on the right side, default is NO.
    
    @IBInspectable public var iconOnRight:Bool = false
    
    //      Whether use square icon, default is NO.
    
    @IBInspectable public var iconSquare:Bool = false
    
    //      Image for radio button icon (optional).
    
    @IBInspectable public var icon:UIImage!
    
    //      Image for radio button icon when selected (optional).
    
    @IBInspectable public var iconSelected:UIImage!
    
    //      Whether enable multiple selection, default is NO.
    
    @IBInspectable public var multipleSelectionEnabled:Bool = false
    
    var isChaining:Bool = false
    
    private var setOtherButtons:NSArray {
        
        get{
            return otherButtons!
        }
        set (newValue) {
            if !isChaining {
                otherButtons = newValue as? Array<ISRadioButton>
                isChaining = true
                for radioButton in otherButtons!{
                    let others:NSMutableArray = NSMutableArray(array:otherButtons!)
                    others.addObject(self)
                    others.removeObject(radioButton)
                    radioButton.setOtherButtons = others
                }
                isChaining = false
            }
        }
    }
    
    @IBInspectable public var setIcon:UIImage {
        
        // Avoid to use getter it can be nill
        
        get{
            return icon
        }
        
        set (newValue){
            icon = newValue
            self.setImage(icon, forState: .Normal)
        }
    }
    
    @IBInspectable public var setIconSelected:UIImage {
        
        // Avoid to use getter it can be nill
        
        get{
            return iconSelected
        }
        
        set (newValue){
            iconSelected = newValue
            self.setImage(iconSelected, forState: .Selected)
            self.setImage(iconSelected, forState: .Highlighted)
        }
    }
    
    var setMultipleSelectionEnabled:Bool {
        
        get{
            return multipleSelectionEnabled
        }
        set (newValue) {
            if !isChaining {
                isChaining = true
                multipleSelectionEnabled = newValue
                
                if self.otherButtons != nil {
                    for radioButton in self.otherButtons!{
                        radioButton.multipleSelectionEnabled = newValue
                    }
                }
                isChaining = false
            }
        }
    }
    
    // MARK: -- Helpers
    
    func drawButton (){
        if (icon == nil ||  self.icon.accessibilityIdentifier == "Generated Icon"){
            self.setIcon = self.drawIconWithSelection(false)
        }else{
            self.setIcon = icon
        }
        if (iconSelected == nil ||  self.iconSelected.accessibilityIdentifier == "Generated Icon"){
            self.setIconSelected = self.drawIconWithSelection(true)
        }else{
            self.setIconSelected = iconSelected
        }
        
        if self.otherButtons != nil {
            self.setOtherButtons = self.otherButtons!
        }
        
        if multipleSelectionEnabled {
            self.setMultipleSelectionEnabled = multipleSelectionEnabled
        }
        
        if self.iconOnRight {
            self.imageEdgeInsets = UIEdgeInsetsMake(0, self.frame.size.width - self.icon.size.width + marginWidth, 0, 0);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, marginWidth + self.icon.size.width);
            self.contentHorizontalAlignment = .Right
        } else {
            self.titleEdgeInsets = UIEdgeInsetsMake(0, marginWidth, 0, 0);
            self.titleLabel?.textAlignment = .Left
            self.contentHorizontalAlignment = .Left
        }
        self.titleLabel?.adjustsFontSizeToFitWidth = true
    }
    
    func drawIconWithSelection (selected:Bool) -> UIImage{
        let rect:CGRect = CGRectMake(0, 0, iconSize, iconSize)
        
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0);
        let context  = UIGraphicsGetCurrentContext()
        //        UIGraphicsPushContext(context!)
        // draw icon
        
        var iconPath:UIBezierPath!
        let iconRect:CGRect = CGRectMake(iconStrokeWidth / 2, iconStrokeWidth / 2, iconSize - iconStrokeWidth, iconSize - iconStrokeWidth);
        if self.iconSquare {
            iconPath = UIBezierPath(rect:iconRect )
        } else {
            iconPath = UIBezierPath(ovalInRect:iconRect)
        }
        iconColor.setStroke()
        iconPath.lineWidth = iconStrokeWidth;
        iconPath.stroke()
        CGContextAddPath(context, iconPath.CGPath);
        
        // draw indicator
        if (selected) {<commit_msg>Do not adjust label's font size to fit width<commit_after>//
//  ISRadioButton.swift
//  DaynamicForm
//
//  Created by <NAME> on 09/12/2015.
//  Copyright © 2015 TheGoal. All rights reserved.
//

import UIKit

@IBDesignable
public class  ISRadioButton: UIButton {
    
    var indexPath:NSIndexPath!
    
    //      Container for holding other buttons in same group.
    
    @IBOutlet var otherButtons: Array<ISRadioButton>?
    
    //      Size of icon, default is 15.0.
    
    @IBInspectable public var iconSize:CGFloat = 15.0
    
    //    Size of selection indicator, default is iconSize * 0.5.
    
    @IBInspectable public var indicatorSize:CGFloat = 15.0 * 0.5
    
    //      Color of icon, default is black
    
    @IBInspectable public var iconColor:UIColor =  UIColor.blackColor()
    
    //      Stroke width of icon, default is iconSize / 9.
    
    @IBInspectable public var iconStrokeWidth:CGFloat = 1.6
    
    //      Color of selection indicator, default is black
    
    @IBInspectable public var indicatorColor:UIColor = UIColor.blackColor()
    
    //      Margin width between icon and title, default is 10. 0.
    
    @IBInspectable public var marginWidth:CGFloat = 10.0
    
    //      Whether icon on the right side, default is NO.
    
    @IBInspectable public var iconOnRight:Bool = false
    
    //      Whether use square icon, default is NO.
    
    @IBInspectable public var iconSquare:Bool = false
    
    //      Image for radio button icon (optional).
    
    @IBInspectable public var icon:UIImage!
    
    //      Image for radio button icon when selected (optional).
    
    @IBInspectable public var iconSelected:UIImage!
    
    //      Whether enable multiple selection, default is NO.
    
    @IBInspectable public var multipleSelectionEnabled:Bool = false
    
    var isChaining:Bool = false
    
    private var setOtherButtons:NSArray {
        
        get{
            return otherButtons!
        }
        set (newValue) {
            if !isChaining {
                otherButtons = newValue as? Array<ISRadioButton>
                isChaining = true
                for radioButton in otherButtons!{
                    let others:NSMutableArray = NSMutableArray(array:otherButtons!)
                    others.addObject(self)
                    others.removeObject(radioButton)
                    radioButton.setOtherButtons = others
                }
                isChaining = false
            }
        }
    }
    
    @IBInspectable public var setIcon:UIImage {
        
        // Avoid to use getter it can be nill
        
        get{
            return icon
        }
        
        set (newValue){
            icon = newValue
            self.setImage(icon, forState: .Normal)
        }
    }
    
    @IBInspectable public var setIconSelected:UIImage {
        
        // Avoid to use getter it can be nill
        
        get{
            return iconSelected
        }
        
        set (newValue){
            iconSelected = newValue
            self.setImage(iconSelected, forState: .Selected)
            self.setImage(iconSelected, forState: .Highlighted)
        }
    }
    
    var setMultipleSelectionEnabled:Bool {
        
        get{
            return multipleSelectionEnabled
        }
        set (newValue) {
            if !isChaining {
                isChaining = true
                multipleSelectionEnabled = newValue
                
                if self.otherButtons != nil {
                    for radioButton in self.otherButtons!{
                        radioButton.multipleSelectionEnabled = newValue
                    }
                }
                isChaining = false
            }
        }
    }
    
    // MARK: -- Helpers
    
    func drawButton (){
        if (icon == nil ||  self.icon.accessibilityIdentifier == "Generated Icon"){
            self.setIcon = self.drawIconWithSelection(false)
        }else{
            self.setIcon = icon
        }
        if (iconSelected == nil ||  self.iconSelected.accessibilityIdentifier == "Generated Icon"){
            self.setIconSelected = self.drawIconWithSelection(true)
        }else{
            self.setIconSelected = iconSelected
        }
        
        if self.otherButtons != nil {
            self.setOtherButtons = self.otherButtons!
        }
        
        if multipleSelectionEnabled {
            self.setMultipleSelectionEnabled = multipleSelectionEnabled
        }
        
        if self.iconOnRight {
            self.imageEdgeInsets = UIEdgeInsetsMake(0, self.frame.size.width - self.icon.size.width + marginWidth, 0, 0);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, marginWidth + self.icon.size.width);
            self.contentHorizontalAlignment = .Right
        } else {
            self.titleEdgeInsets = UIEdgeInsetsMake(0, marginWidth, 0, 0);
            self.titleLabel?.textAlignment = .Left
            self.contentHorizontalAlignment = .Left
        }
        self.titleLabel?.adjustsFontSizeToFitWidth = false
    }
    
    func drawIconWithSelection (selected:Bool) -> UIImage{
        let rect:CGRect = CGRectMake(0, 0, iconSize, iconSize)
        
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0);
        let context  = UIGraphicsGetCurrentContext()
        //        UIGraphicsPushContext(context!)
        // draw icon
        
        var iconPath:UIBezierPath!
        let iconRect:CGRect = CGRectMake(iconStrokeWidth / 2, iconStrokeWidth / 2, iconSize - iconStrokeWidth, iconSize - iconStrokeWidth);
        if self.iconSquare {
            iconPath = UIBezierPath(rect:iconRect )
        } else {
            iconPath = UIBezierPath(ovalInRect:iconRect)
        }
        iconColor.setStroke()
        iconPath.lineWidth = iconStrokeWidth;
        iconPath.stroke()
        CGContextAddPath(context, iconPath.CGPath);
        
        // draw indicator
        if (selected) {
{'fraction_non_alphanumeric': 0.07432659932659932, 'fraction_numerical': 0.008249158249158249, 'mean_word_length': 1.691053227633069, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7638202', 'n_tokens_mistral': 3309, 'n_tokens_neox': 3209, 'n_words': 782}