XG_TitleView.m 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // XG_TitleView.m
  3. // MyApp
  4. //
  5. // Created by huxinguang on 2018/9/11.
  6. // Copyright © 2018年 huxinguang. All rights reserved.
  7. //
  8. #import "XG_TitleView.h"
  9. #import "XG_PickerMacro.h"
  10. @interface XG_TitleView()
  11. @property (nonatomic, assign) XG_TitleViewStyle style;
  12. @end
  13. @implementation XG_TitleView
  14. - (instancetype)initWithFrame:(CGRect)frame style:(XG_TitleViewStyle)style{
  15. if (self = [super initWithFrame:frame]) {
  16. self.style = style;
  17. [self buildSubViews];
  18. }
  19. return self;
  20. }
  21. - (void)buildSubViews{
  22. self.titleLabel = [[UILabel alloc]init];
  23. self.titleLabel.font = [UIFont boldSystemFontOfSize:kAppNavigationTitleViewTitleFontSize];
  24. self.titleLabel.frame = CGRectMake(0, 0, kAppNavigationTitleViewMaxWidth, kAppNavigationTitleViewHeight);
  25. self.titleLabel.textColor = kAppNavigationTopPageTitleColor;
  26. self.titleLabel.textAlignment = NSTextAlignmentCenter;
  27. [self addSubview:self.titleLabel];
  28. }
  29. - (void)setTitleString:(NSString *)titleString{
  30. if (titleString && titleString.length > 0) {
  31. self.titleLabel.text = titleString;
  32. [self.titleLabel sizeToFit];
  33. CGRect titleLabelFrame = self.titleLabel.frame;
  34. titleLabelFrame.origin.y = (kAppNavigationTitleViewHeight - titleLabelFrame.size.height)/2;
  35. if (titleLabelFrame.size.width > kAppNavigationTitleViewMaxWidth) {
  36. titleLabelFrame.size.width = kAppNavigationTitleViewMaxWidth;
  37. }
  38. self.titleLabel.frame = titleLabelFrame;
  39. // 设置titleview居中显示
  40. CGFloat screenWidth = 0;
  41. if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait){
  42. screenWidth = kAppScreenWidth;
  43. }else{
  44. screenWidth = kAppScreenHeight;
  45. }
  46. self.frame = CGRectMake((screenWidth - titleLabelFrame.size.width)/2, self.frame.origin.y, titleLabelFrame.size.width, kAppNavigationTitleViewHeight);
  47. }
  48. }
  49. /*
  50. // Only override drawRect: if you perform custom drawing.
  51. // An empty implementation adversely affects performance during animation.
  52. - (void)drawRect:(CGRect)rect {
  53. // Drawing code
  54. }
  55. */
  56. @end