UIButton+ex.m 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // UIButton+ex.m
  3. // LNManager
  4. //
  5. // Created by EchoShacolee on 2017/4/13.
  6. // Copyright © 2017年 lee. All rights reserved.
  7. //
  8. #import "UIButton+ex.h"
  9. @implementation UIButton (ex)
  10. -(void)setTitleUnderImgWithTitle:(NSString *)title TitleColor:(UIColor *)color Image:(UIImage *)image label:(UILabel *)topLab{
  11. CGFloat width = self.frame.size.width;
  12. CGFloat height = self.frame.size.height/10;
  13. CGRect topFrame = CGRectMake(width/2-height*4/2, height*2, height*4, height*4);
  14. if (image) {
  15. //图片+文字
  16. UIImageView * imgV = [[UIImageView alloc]initWithFrame:topFrame];
  17. imgV.image = image;
  18. [self addSubview:imgV];
  19. self.layer.borderWidth = .5f;
  20. self.layer.borderColor = [UIColor colorWithRed:230/255.0f green:230/255.0f blue:230/255.0f alpha:1].CGColor;
  21. }else{
  22. //lable+文字
  23. topLab.frame = topFrame;
  24. topLab.font = [UIFont systemFontOfSize:[self scaleFontSize:16]];
  25. topLab.adjustsFontSizeToFitWidth = YES;
  26. topLab.textColor = [UIColor darkGrayColor];
  27. topLab.textAlignment = NSTextAlignmentCenter;
  28. // topLab.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
  29. topLab.layer.borderWidth = .5f;
  30. topLab.layer.borderColor = [UIColor lightGrayColor].CGColor;
  31. topLab.layer.cornerRadius = 5;
  32. topLab.layer.masksToBounds = YES;
  33. [self addSubview:topLab];
  34. UILabel * Vline = [[UILabel alloc]initWithFrame:CGRectMake(width-1, height*2, 1, height*6)];
  35. Vline.backgroundColor = RGB_COLOR(230, 230, 230);
  36. [self addSubview:Vline];
  37. }
  38. UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(0, height*6, width, height*3)];
  39. lab.text = title;
  40. lab.textAlignment = NSTextAlignmentCenter;
  41. lab.textColor = [UIColor darkGrayColor];
  42. lab.font = [UIFont systemFontOfSize:[self scaleFontSize:14]];
  43. lab.numberOfLines = 2;//
  44. if (topLab) {
  45. lab.textColor = [UIColor grayColor];
  46. }
  47. [self addSubview:lab];
  48. }
  49. -(void)setTitle:(NSString*)title textColor:(UIColor*)color font:(CGFloat)font fotState:(UIControlState)stateType
  50. {
  51. [self setTitle:title forState:stateType];
  52. [self setTitleColor:color forState:stateType];
  53. [self.titleLabel setFont:[UIFont systemFontOfSize:font]];
  54. }
  55. -(CGFloat)scaleFontSize:(CGFloat)size{
  56. if (kSize.width < 414) {
  57. return size-1;
  58. }
  59. return size;
  60. }
  61. -(void)borderColor:(UIColor *)color width:(CGFloat)wid cornorRadios:(CGFloat)cornor
  62. {
  63. self.layer.masksToBounds = YES;
  64. self.layer.borderWidth = wid;
  65. self.layer.cornerRadius = cornor;
  66. self.layer.borderColor = color.CGColor;
  67. }
  68. @end