MyGroupCourseGroupTipsView.m 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //
  2. // MyGroupCourseGroupTipsView.m
  3. // KulexiuForStudent
  4. //
  5. // Created by 王智 on 2024/12/24.
  6. //
  7. #import "MyGroupCourseGroupTipsView.h"
  8. @interface MyGroupCourseGroupTipsView ()
  9. @property (weak, nonatomic) IBOutlet UILabel *tipsLabel;
  10. @end
  11. @implementation MyGroupCourseGroupTipsView
  12. + (instancetype)sharedInstance {
  13. MyGroupCourseGroupTipsView *view = [[[NSBundle mainBundle] loadNibNamed:@"MyGroupCourseGroupTipsView" owner:nil options:nil] firstObject];
  14. return view;
  15. }
  16. - (void)configWithMinStudentCount:(NSInteger)count {
  17. NSString *descString = [NSString stringWithFormat:@"该小组课销售截止后,报名人数若少于%zd人将取消开课,已购买学员付费金额将自动返还,请您放心购买",count];
  18. NSMutableParagraphStyle *paragraphStyle = [MyGroupCourseGroupTipsView getParagraphStyleWithFont:[UIFont systemFontOfSize:12.0f] lineHeight:23.0f];
  19. CGFloat baselineOffset = [MyGroupCourseGroupTipsView getBaseOffline:[UIFont systemFontOfSize:12.0f] lineHeight:23.0f];
  20. NSMutableAttributedString *introduceAttrs = [[NSMutableAttributedString alloc] initWithString:descString attributes:@{NSParagraphStyleAttributeName:paragraphStyle, NSBaselineOffsetAttributeName:@(baselineOffset),NSFontAttributeName:[UIFont systemFontOfSize:12.0f], NSForegroundColorAttributeName:HexRGB(0x777777)}];
  21. self.tipsLabel.attributedText = introduceAttrs;
  22. }
  23. + (CGFloat)getViewHeightWithCount:(NSInteger)count {
  24. NSString *descString = [NSString stringWithFormat:@"该小组课销售截止后,报名人数若少于%zd人将取消开课,已购买学员付费金额将自动返还,请您放心购买",count];
  25. return 54 + 30 + [self getViewRealHeight:descString];
  26. }
  27. + (CGFloat)getBaseOffline:(UIFont *)font lineHeight:(CGFloat)lineHeight {
  28. CGFloat descender = fabs(font.descender); // descender 为负值,取绝对值
  29. CGFloat ascender = font.ascender;
  30. CGFloat fontHeight = ascender + descender;
  31. CGFloat offset = (lineHeight - fontHeight) / 2.0; // 假设上下均等分布
  32. return offset;
  33. }
  34. + (NSMutableParagraphStyle *)getParagraphStyleWithFont:(UIFont *)font lineHeight:(CGFloat)lineHeigh {
  35. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  36. paragraphStyle.maximumLineHeight = lineHeigh;
  37. paragraphStyle.minimumLineHeight = lineHeigh;
  38. return paragraphStyle;
  39. }
  40. + (CGFloat)getViewRealHeight:(NSString *)descString {
  41. NSMutableParagraphStyle *paragraphStyle = [self getParagraphStyleWithFont:[UIFont systemFontOfSize:12.0f] lineHeight:23.0f];
  42. CGFloat baselineOffset = [self getBaseOffline:[UIFont systemFontOfSize:12.0f] lineHeight:23.0f];
  43. CGFloat height = [descString boundingRectWithSize:CGSizeMake(KPortraitWidth - 26, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSParagraphStyleAttributeName:paragraphStyle, NSBaselineOffsetAttributeName:@(baselineOffset),NSFontAttributeName:[UIFont systemFontOfSize:12.0f]} context:nil].size.height + 1;
  44. return height;
  45. }
  46. /*
  47. // Only override drawRect: if you perform custom drawing.
  48. // An empty implementation adversely affects performance during animation.
  49. - (void)drawRect:(CGRect)rect {
  50. // Drawing code
  51. }
  52. */
  53. @end