@@ -16,11 +16,17 @@ public class LGButton: UIControl {
1616 case touched = 0.7
1717 case untouched = 1.0
1818 }
19-
19+
2020 let touchDisableRadius : CGFloat = 100.0
2121
22- let availableFontIcons = [ " fa " , " io " , " oc " , " ic " , " ma " , " ti " , " mi " ]
23-
22+ private var availableFontIcons : [ String : IconFont ] = [ " fa " : Fonts . awesome,
23+ " io " : Fonts . ion,
24+ " oc " : Fonts . oct,
25+ " ic " : Fonts . ic,
26+ " ma " : Fonts . material,
27+ " ti " : Fonts . themify,
28+ " mi " : Fonts . map]
29+
2430 var gradient : CAGradientLayer ?
2531
2632
@@ -53,7 +59,7 @@ public class LGButton: UIControl {
5359
5460 public var isLoading = false {
5561 didSet {
56- showLoadingView ( )
62+ showLoadingView ( )
5763 }
5864 }
5965
@@ -148,6 +154,12 @@ public class LGButton: UIControl {
148154 }
149155 }
150156
157+ @IBInspectable public var titleNumOfLines : Int = 1 {
158+ didSet{
159+ setupView ( )
160+ }
161+ }
162+
151163 @IBInspectable public var verticalOrientation : Bool = false {
152164 didSet {
153165 setupView ( )
@@ -334,16 +346,38 @@ public class LGButton: UIControl {
334346 }
335347 }
336348
349+ @IBInspectable public var leftAligned : Bool = false {
350+ didSet{
351+ setupView ( )
352+ }
353+ }
354+
355+ @IBInspectable public var rightAligned : Bool = false {
356+ didSet{
357+ setupView ( )
358+ }
359+ }
360+
361+ // MARK: - Standard Properties
362+ // MARK:
363+ public var attributedString : NSAttributedString ? {
364+ didSet {
365+ titleLbl. attributedText = attributedString
366+ }
367+ }
368+
337369 // MARK: - Overrides
338370 // MARK:
339371 override init ( frame: CGRect ) {
340372 super. init ( frame: frame)
373+ registIconFont ( )
341374 xibSetup ( )
342375 setupView ( )
343376 }
344377
345378 required public init ( coder aDecoder: NSCoder ) {
346379 super. init ( coder: aDecoder) !
380+ registIconFont ( )
347381 xibSetup ( )
348382 setupView ( )
349383 }
@@ -357,6 +391,18 @@ public class LGButton: UIControl {
357391 setupBorderAndCorners ( )
358392 }
359393
394+ override public func awakeFromNib( ) {
395+ super. awakeFromNib ( )
396+ xibSetup ( )
397+ setupView ( )
398+ }
399+
400+ override public func prepareForInterfaceBuilder( ) {
401+ super. prepareForInterfaceBuilder ( )
402+ xibSetup ( )
403+ setupView ( )
404+ }
405+
360406 override public var intrinsicContentSize : CGSize {
361407 return CGSize ( width: 10 , height: 10 )
362408 }
@@ -369,17 +415,24 @@ public class LGButton: UIControl {
369415 bgContentView. clipsToBounds = true
370416 layer. masksToBounds = false
371417 setIconOrientation ( )
418+ setupTitle ( )
372419 setupBackgroundColor ( )
373420 setupGradientBackground ( )
374421 setupBorderAndCorners ( )
375- setupTitle ( )
376422 setupLeftIcon ( )
377423 setupRightIcon ( )
378424 setupLeftImage ( )
379425 setupRightImage ( )
380426 setupSpacings ( )
381427 setupShadow ( )
382428 setupLoadingView ( )
429+ setupAlignment ( )
430+ }
431+
432+ fileprivate func registIconFont( ) {
433+ for (key, value) in self . availableFontIcons {
434+ SwiftIconFont . registFont ( from: value, name: key)
435+ }
383436 }
384437
385438 fileprivate func setIconOrientation( ) {
@@ -416,7 +469,7 @@ public class LGButton: UIControl {
416469 let d = pow ( sinf ( ( 2 * Float( Double . pi) * ( ( xAngle+ 0.5 ) / 2 ) ) ) , 2 )
417470 gradient!. startPoint = CGPoint ( x: CGFloat ( a) , y: CGFloat ( b) )
418471 gradient!. endPoint = CGPoint ( x: CGFloat ( c) , y: CGFloat ( d) )
419-
472+
420473 bgContentView. layer. addSublayer ( gradient!)
421474 }
422475 }
@@ -435,6 +488,7 @@ public class LGButton: UIControl {
435488
436489 fileprivate func setupTitle( ) {
437490 titleLbl. isHidden = titleString. isEmpty
491+ titleLbl. numberOfLines = titleNumOfLines
438492 titleLbl. text = titleString
439493 titleLbl. textColor = titleColor
440494 if titleFontName != nil {
@@ -468,7 +522,7 @@ public class LGButton: UIControl {
468522 heightConstraint: leftImageHeightConstraint,
469523 widthValue: leftImageWidth,
470524 heightValue: leftImageHeight)
471- leftIcon. isHidden = ( leftImageSrc != nil || !availableFontIcons . contains ( leftIconFontName) )
525+ leftIcon. isHidden = ( leftImageSrc != nil || !( SwiftIconFont . fonts [ leftIconFontName] != nil ) )
472526 }
473527
474528 fileprivate func setupRightImage( ) {
@@ -480,7 +534,7 @@ public class LGButton: UIControl {
480534 heightConstraint: rightImageHeightConstraint,
481535 widthValue: rightImageWidth,
482536 heightValue: rightImageHeight)
483- rightIcon. isHidden = ( rightImageSrc != nil || !availableFontIcons . contains ( rightIconFontName) )
537+ rightIcon. isHidden = ( rightImageSrc != nil || !( SwiftIconFont . fonts [ rightIconFontName] != nil ) )
484538 }
485539
486540 fileprivate func setupSpacings( ) {
@@ -512,44 +566,42 @@ public class LGButton: UIControl {
512566 setupBorderAndCorners ( )
513567 }
514568
569+ fileprivate func setupAlignment( ) {
570+ if leftAligned {
571+ titleLbl. textAlignment = . left
572+ } else if rightAligned {
573+ titleLbl. textAlignment = . right
574+ } else {
575+ titleLbl. textAlignment = . center
576+ }
577+ }
578+
515579 fileprivate func setupIcon( icon: UILabel , fontName: String , iconName: String , fontSize: CGFloat , color: UIColor ) {
516- icon. isHidden = !availableFontIcons. contains ( fontName)
517- if !icon. isHidden {
580+ defer {
581+ setupBorderAndCorners ( )
582+ }
583+ if let iconFont = SwiftIconFont . fonts [ fontName] {
584+ icon. isHidden = false
518585 icon. textColor = color
519- switch fontName {
520- case " fa " :
521- icon. font = UIFont . icon ( from: . FontAwesome, ofSize: fontSize)
522- icon. text = String . fontAwesomeIcon ( iconName)
523- break ;
524- case " io " :
525- icon. font = UIFont . icon ( from: . Ionicon, ofSize: fontSize)
526- icon. text = String . fontIonIcon ( iconName)
527- break ;
528- case " oc " :
529- icon. font = UIFont . icon ( from: . Octicon, ofSize: fontSize)
530- icon. text = String . fontOcticon ( iconName)
531- break ;
532- case " ic " :
533- icon. font = UIFont . icon ( from: . Iconic, ofSize: fontSize)
534- icon. text = String . fontIconicIcon ( iconName)
535- break ;
536- case " ma " :
537- icon. font = UIFont . icon ( from: . MaterialIcon, ofSize: fontSize)
538- icon. text = String . fontMaterialIcon ( iconName. replacingOccurrences ( of: " - " , with: " . " ) )
539- break ;
540- case " ti " :
541- icon. font = UIFont . icon ( from: . Themify, ofSize: fontSize)
542- icon. text = String . fontThemifyIcon ( iconName. replacingOccurrences ( of: " - " , with: " . " ) )
543- break ;
544- case " mi " :
545- icon. font = UIFont . icon ( from: . MapIcon, ofSize: fontSize)
546- icon. text = String . fontMapIcon ( iconName. replacingOccurrences ( of: " - " , with: " . " ) )
547- break ;
548- default :
549- break ;
586+ icon. font = UIFont . icon ( from: iconFont, ofSize: fontSize)
587+ if let iconStr = String . getIcon ( from: iconFont, code: iconName) {
588+ icon. text = iconStr
589+ } else {
590+ let joind = [ " - " , " . " , " _ " ]
591+ for left in joind {
592+ for right in joind {
593+ if left == right { continue }
594+ let code = iconName. replacingOccurrences ( of: left, with: right)
595+ if let iconStr = String . getIcon ( from: iconFont, code: code) {
596+ icon. text = iconStr
597+ return
598+ }
599+ }
600+ }
550601 }
602+ } else {
603+ icon. isHidden = true
551604 }
552- setupBorderAndCorners ( )
553605 }
554606
555607 fileprivate func setupImage( imageView: UIImageView , image: UIImage ? , color: UIColor ? , widthConstraint: NSLayoutConstraint , heightConstraint: NSLayoutConstraint , widthValue: CGFloat , heightValue: CGFloat ) {
@@ -581,7 +633,7 @@ public class LGButton: UIControl {
581633 fileprivate func xibSetup( ) {
582634 rootView = loadViewFromNib ( )
583635 rootView. frame = bounds
584- rootView. autoresizingMask = [ UIViewAutoresizing . flexibleWidth, UIViewAutoresizing . flexibleHeight]
636+ rootView. autoresizingMask = [ . flexibleWidth, . flexibleHeight]
585637 addSubview ( rootView)
586638 leadingLoadingConstraint. isActive = false
587639 trailingLoadingConstraint. isActive = false
@@ -613,7 +665,7 @@ public class LGButton: UIControl {
613665 touchAlpha = ( pressed) ? . touched : . untouched
614666 }
615667 }
616-
668+
617669 override public func touchesBegan( _ touches: Set < UITouch > , with event: UIEvent ? ) {
618670 pressed = true
619671 }
@@ -651,4 +703,12 @@ public class LGButton: UIControl {
651703 }
652704 }
653705 }
706+
707+ @IBAction func tapAction( _ sender: Any ) {
708+ let shouldSendActions = pressed
709+ pressed = false
710+ if shouldSendActions{
711+ sendActions ( for: . touchUpInside)
712+ }
713+ }
654714}
0 commit comments