STButton.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // STButton.m
  3. // test12
  4. //
  5. // Created by apple on 15/11/14.
  6. // Copyright (c) 2015年 JCZ. All rights reserved.
  7. //
  8. #import "STButton.h"
  9. #import <UIKit/UIKit.h>
  10. @interface STButton()
  11. {
  12. UILabel* subLable;
  13. }
  14. @end
  15. @implementation STButton
  16. -(instancetype)initWithFrame:(CGRect)frame
  17. {
  18. self = [super initWithFrame:frame];
  19. if (self) {
  20. [self setTitleColor:contentTextColor forState:UIControlStateNormal];
  21. [self.titleLabel setTextAlignment:NSTextAlignmentCenter];
  22. [self.imageView setContentMode:UIViewContentModeScaleAspectFit];
  23. self.titleLabel.numberOfLines = 0;
  24. }
  25. return self;
  26. }
  27. /**
  28. 这里Y有2中,一种是wid较大的。一种是hei较大的。
  29. 加入title占比。4.
  30. 现在的问题是,当图片小时,Y差距过大。
  31. [lastBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
  32. [lastBtn setContentVerticalAlignment:UIControlContentVerticalAlignmentTop];
  33. */
  34. /**其实有三种button。普通的。导航栏的。有子标题的。
  35. */
  36. -(CGRect)imageRectForContentRect:(CGRect)contentRect
  37. {
  38. CGFloat hei = self.frame.size.height;
  39. CGFloat wid = self.frame.size.width;
  40. CGFloat len = hei < wid ? hei:wid;
  41. CGFloat ivWH = len*.45;
  42. if (_style == 1) {
  43. return CGRectMake( (wid - ivWH)/2, hei*.1, ivWH, ivWH);
  44. }
  45. if (_style == 2) {
  46. return CGRectMake( 0, hei* .1, wid,hei* .6);
  47. }
  48. if (hei < wid && hei > 50) {
  49. ivWH = len* .3;
  50. return CGRectMake( (wid - ivWH)/2, hei*.2, ivWH, ivWH);
  51. }
  52. return CGRectMake( (wid - ivWH)/2, (hei - len)/2, ivWH, ivWH);
  53. }
  54. -(CGRect)titleRectForContentRect:(CGRect)contentRect
  55. {
  56. CGFloat hei = self.frame.size.height;
  57. CGFloat wid = self.frame.size.width;
  58. CGFloat len = hei < wid ? hei:wid;
  59. if (1 == _style) {
  60. return CGRectMake( 0, len * .6, wid, len*.25);
  61. }
  62. if (2 == _style) {
  63. return CGRectMake( 0, hei*.75, wid, hei*.25);
  64. }
  65. return CGRectMake( 0, (hei - len)/2 + len*.5, wid, len*.3);
  66. // return CGRectMake( 0, (hei - len)/2 + len*.6, wid, len*.2);
  67. }
  68. -(void)setSubTitle:(NSString *)subTitle
  69. {
  70. CGFloat hei = self.frame.size.height;
  71. CGFloat wid = self.frame.size.width;
  72. CGFloat len = hei < wid ? hei:wid;
  73. UILabel* label = [[UILabel alloc] init];
  74. [label setFrame:CGRectMake( 0, (hei - len)/2 + len*.8, wid, len*.2)];
  75. [label setTextAlignment:NSTextAlignmentCenter];
  76. [label setTextColor: contentTextColor];
  77. [label setText:subTitle];
  78. [self addSubview:label];
  79. if (self.titleLabel.font) {
  80. [label setFont:self.titleLabel.font];
  81. }else{
  82. [label setFont:[UIFont scaleSize:14]];
  83. }
  84. subLable = label;
  85. }
  86. - (void)setSubTitle:(NSString *)subTitle Font:(CGFloat)ft color:(UIColor *)col {
  87. [self setSubTitle:subTitle];
  88. [subLable setFont:[UIFont scaleSize:ft]];
  89. [subLable setTextColor:col];
  90. }
  91. @end