WardPrivilegeButton.m 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //
  2. // WardPrivilegeButton.m
  3. // BuguLive
  4. //
  5. // Created by 范东 on 2019/2/1.
  6. // Copyright © 2019 xfg. All rights reserved.
  7. //
  8. #import "WardPrivilegeButton.h"
  9. @interface WardPrivilegeButton()
  10. @property (nonatomic, strong) UIImageView *imageView;
  11. @property (nonatomic, strong) UILabel *titleLabel;
  12. @end
  13. @implementation WardPrivilegeButton
  14. - (instancetype)initWithFrame:(CGRect)frame{
  15. if (self = [super initWithFrame:frame]) {
  16. [self addSubview:self.imageView];
  17. [self addSubview:self.titleLabel];
  18. }
  19. return self;
  20. }
  21. - (void)setImageUrl:(NSString *)imageUrl{
  22. [SDWebImageDownloader.sharedDownloader setValue:@"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
  23. forHTTPHeaderField:@"Accept"];
  24. [self.imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  25. if (image) {
  26. [self.imageView setImage:image];
  27. self.imageView.size = CGSizeMake(self.width / 2, self.width / 2);
  28. self.imageView.centerX = self.width / 2;
  29. self.imageView.bottom = self.titleLabel.top - 10;
  30. }
  31. }];
  32. }
  33. - (void)setTitle:(NSString *)title{
  34. [self.titleLabel setText:title];
  35. CGSize titleLabelSize = [title textSizeIn:CGSizeMake(MAXFLOAT, MAXFLOAT) font:[UIFont systemFontOfSize:15]];
  36. self.titleLabel.size = titleLabelSize;
  37. self.titleLabel.centerX = self.width / 2;
  38. }
  39. - (UIImageView *)imageView{
  40. if (!_imageView) {
  41. _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
  42. _imageView.centerX = self.centerX;
  43. _imageView.top = 10;
  44. _imageView.contentMode = UIViewContentModeScaleAspectFit;
  45. _imageView.clipsToBounds = YES;
  46. }
  47. return _imageView;
  48. }
  49. - (UILabel *)titleLabel{
  50. if (!_titleLabel) {
  51. _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, self.imageView.bottom + 10, 0, 0)];
  52. _titleLabel.textColor = [UIColor colorWithHexString:@"#333333"];
  53. _titleLabel.font = [UIFont systemFontOfSize:15];
  54. _titleLabel.centerX = self.width / 2;
  55. _titleLabel.bottom = self.height - 20;
  56. }
  57. return _titleLabel;
  58. }
  59. @end