CityButton.m 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // CityButton.m
  3. // ChinaCityList
  4. //
  5. // Created by zjq on 15/10/27.
  6. // Copyright © 2015年 zhengjq. All rights reserved.
  7. //
  8. #import "CityButton.h"
  9. #define IsNilOrNull(_ref) (((_ref) == nil) || ([(_ref) isEqual:[NSNull null]]))
  10. #define kCityItemViewTitleTextW 80
  11. #define kCityItemViewTitleTextH 25
  12. #define kCityItemViewTitleTextFont 15
  13. @interface CityButton ()
  14. @property (nonatomic, weak) UIView *container;
  15. @property (nonatomic, weak) UILabel *titleName;
  16. @end
  17. @implementation CityButton
  18. - (id)init
  19. {
  20. if (self = [super init]) {
  21. // Custom initialization
  22. self.backgroundColor = [UIColor whiteColor];
  23. self.layer.masksToBounds = YES;
  24. self.layer.cornerRadius = self.frame.size.width/20;
  25. self.layer.borderColor = [UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1].CGColor;
  26. self.layer.borderWidth = 1;
  27. [self setupViews];
  28. }
  29. return self;
  30. }
  31. - (void)setupViews {
  32. UIView *container = [[UIView alloc] init];
  33. container.userInteractionEnabled = NO;
  34. container.backgroundColor = [UIColor clearColor];
  35. UILabel *titleName = [[UILabel alloc] init];
  36. titleName.font = [UIFont systemFontOfSize:kCityItemViewTitleTextFont];
  37. titleName.textColor = [UIColor grayColor];
  38. titleName.textAlignment = NSTextAlignmentCenter;
  39. titleName.backgroundColor = [UIColor clearColor];
  40. titleName.numberOfLines = 1;
  41. [container addSubview:titleName];
  42. self.titleName = titleName;
  43. [self addSubview:container];
  44. self.container = container;
  45. }
  46. - (void)layoutSubviews {
  47. [super layoutSubviews];
  48. self.container.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
  49. self.titleName.frame = CGRectMake(0, 0, self.container.frame.size.width, self.container.frame.size.height);
  50. }
  51. - (void)setCityItem:(CityItem *)cityItem
  52. {
  53. _cityItem = cityItem;
  54. if (!IsNilOrNull(_cityItem)) {
  55. self.titleName.text = _cityItem.titleName;
  56. // [self setTitle:_cityItem.titleName forState:UIControlStateNormal];
  57. }
  58. }
  59. @end