YMEmptyView.m 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. //
  2. // YMEmptyView.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2023/11/6.
  6. //
  7. #import "YMEmptyView.h"
  8. //每个子控件之间的间距
  9. #define kYMEmptySubViewMargin 20.f
  10. //描述字体
  11. #define kYMEmptyTitleLabFont [UIFont systemFontOfSize:16.f]
  12. //详细描述字体
  13. #define kYMEmptyDetailLabFont [UIFont systemFontOfSize:14.f]
  14. //按钮字体大小
  15. #define kYMEmptyActionBtnFont [UIFont systemFontOfSize:14.f]
  16. //按钮高度
  17. #define kYMEmptyActionBtnHeight 40.f
  18. //按钮宽度
  19. #define kYMEmptyActionBtnWidth 120.f
  20. //水平方向内边距
  21. #define kYMEmptyActionBtnHorizontalMargin 30.f
  22. //背景色
  23. #define kYMEmptyBackgroundColor [UIColor colorWithRed:250.f/255.f green:250.f/255.f blue:250.f/255.f alpha:1.f]
  24. //黑色
  25. #define kYMEmptyBlackColor [UIColor colorWithRed:0.3f green:0.3f blue:0.3f alpha:1.f]
  26. //灰色
  27. #define kYMEmptyGrayColor [UIColor colorWithRed:0.5f green:0.5f blue:0.5f alpha:1.f]
  28. @interface YMEmptyView ()
  29. @property (nonatomic, strong) UIImageView *promptImageView;
  30. @property (nonatomic, strong) UILabel *titleLabel;
  31. @property (nonatomic, strong) UILabel *detailLabel;
  32. @property (nonatomic, strong) UIButton *actionButton;
  33. @property (nonatomic, strong) UIView *customV;
  34. @end
  35. @implementation YMEmptyView
  36. {
  37. CGFloat contentMaxWidth; //最大宽度
  38. CGFloat contentWidth; //内容物宽度
  39. CGFloat contentHeight; //内容物高度
  40. CGFloat subViweMargin; //内容物上每个子控件之间的间距
  41. }
  42. - (void)initialize{
  43. self.actionBtnHeight = 40.f;
  44. self.actionBtnWidth = 120.f;
  45. self.actionBtnHorizontalMargin = 30.f;
  46. self.detailLabMaxLines = 2;
  47. }
  48. - (void)prepare{
  49. [super prepare];
  50. self.autoShowEmptyView = YES; //默认自动显隐
  51. self.contentViewY = 1000; //默认值,用来判断是否设置过content的Y值
  52. }
  53. - (void)setupSubviews{
  54. [super setupSubviews];
  55. contentMaxWidth = self.emptyViewIsCompleteCoverSuperView ? self.ym_empty_width : self.ym_empty_width - 30.f;
  56. contentWidth = 0;//内容物宽度
  57. contentHeight = 0;//内容物高度
  58. subViweMargin = self.subViewMargin ? self.subViewMargin : kYMEmptySubViewMargin;
  59. //占位图片
  60. UIImage *image;
  61. if (self.imageStr.length) {
  62. image = [UIImage imageNamed:self.imageStr];
  63. }
  64. if(self.image){
  65. [self setupPromptImageView:self.image];
  66. }else if (image) {
  67. [self setupPromptImageView:image];
  68. } else{
  69. if (_promptImageView) {
  70. [self.promptImageView removeFromSuperview];
  71. self.promptImageView = nil;
  72. }
  73. }
  74. //标题
  75. if (self.titleStr.length) {
  76. [self setupTitleLabel:self.titleStr];
  77. }else{
  78. if (_titleLabel) {
  79. [self.titleLabel removeFromSuperview];
  80. self.titleLabel = nil;
  81. }
  82. }
  83. //详细描述
  84. if (self.detailStr.length) {
  85. [self setupDetailLabel:self.detailStr];
  86. }else{
  87. if (_detailLabel) {
  88. [self.detailLabel removeFromSuperview];
  89. self.detailLabel = nil;
  90. }
  91. }
  92. //按钮
  93. if (self.btnTitleStr.length) {
  94. if (self.actionBtnTarget && self.actionBtnAction) {
  95. [self setupActionBtn:self.btnTitleStr target:self.actionBtnTarget action:self.actionBtnAction btnClickBlock:nil];
  96. }else if (self.btnClickBlock) {
  97. [self setupActionBtn:self.btnTitleStr target:nil action:nil btnClickBlock:self.btnClickBlock];
  98. }else{
  99. if (_actionButton) {
  100. [self.actionButton removeFromSuperview];
  101. self.actionButton = nil;
  102. }
  103. }
  104. }else{
  105. if (_actionButton) {
  106. [self.actionButton removeFromSuperview];
  107. self.actionButton = nil;
  108. }
  109. }
  110. //自定义view
  111. if (self.customView) {
  112. contentWidth = self.customView.ym_empty_width;
  113. contentHeight = self.customView.ym_empty_maxY;
  114. }
  115. ///设置frame
  116. [self setSubViewFrame];
  117. }
  118. - (void)setSubViewFrame{
  119. //emptyView初始宽高
  120. CGFloat originEmptyWidth = self.ym_empty_width;
  121. CGFloat originEmptyHeight = self.ym_empty_height;
  122. CGFloat emptyViewCenterX = originEmptyWidth * 0.5f;
  123. CGFloat emptyViewCenterY = originEmptyHeight * 0.5f;
  124. //不是完全覆盖父视图时,重新设置self的frame(大小为content的大小)
  125. if (!self.emptyViewIsCompleteCoverSuperView) {
  126. self.ym_empty_size = CGSizeMake(contentWidth, contentHeight);
  127. }
  128. self.center = CGPointMake(emptyViewCenterX, emptyViewCenterY);
  129. //设置contentView
  130. self.contentView.ym_empty_size = CGSizeMake(contentWidth, contentHeight);
  131. if (self.emptyViewIsCompleteCoverSuperView) {
  132. self.contentView.center = CGPointMake(emptyViewCenterX, emptyViewCenterY);
  133. } else {
  134. self.contentView.center = CGPointMake(contentWidth*0.5, contentHeight*0.5);
  135. }
  136. //子控件的centerX设置
  137. CGFloat centerX = self.contentView.ym_empty_width * 0.5f;
  138. if (self.customView) {
  139. self.customView.ym_empty_centerX = centerX;
  140. }else{
  141. _promptImageView.ym_empty_centerX = centerX;
  142. _titleLabel.ym_empty_centerX = centerX;
  143. _detailLabel.ym_empty_centerX = centerX;
  144. _actionButton.ym_empty_centerX = centerX;
  145. }
  146. if (self.contentViewOffset) { //有无设置偏移
  147. self.ym_empty_centerY += self.contentViewOffset;
  148. } else if (self.contentViewY < 1000) { //有无设置Y坐标值
  149. self.ym_empty_y = self.contentViewY;
  150. }
  151. //是否忽略scrollView的contentInset
  152. if (self.ignoreContentInset && [self.superview isKindOfClass:[UIScrollView class]]) {
  153. UIScrollView *scrollView = (UIScrollView *)self.superview;
  154. self.ym_empty_centerY -= scrollView.contentInset.top;
  155. self.ym_empty_centerX -= scrollView.contentInset.left;
  156. self.ym_empty_centerY += scrollView.contentInset.bottom;
  157. self.ym_empty_centerX += scrollView.contentInset.right;
  158. }
  159. }
  160. #pragma mark - ------------------ Setup View ------------------
  161. - (void)setupPromptImageView:(UIImage *)img{
  162. self.promptImageView.image = img;
  163. CGFloat imgViewWidth = img.size.width;
  164. CGFloat imgViewHeight = img.size.height;
  165. if (self.imageSize.width && self.imageSize.height) {//设置了宽高大小
  166. if (imgViewWidth > imgViewHeight) {//以宽为基准,按比例缩放高度
  167. imgViewHeight = (imgViewHeight / imgViewWidth) * self.imageSize.width;
  168. imgViewWidth = self.imageSize.width;
  169. }else{//以高为基准,按比例缩放宽度
  170. imgViewWidth = (imgViewWidth / imgViewHeight) * self.imageSize.height;
  171. imgViewHeight = self.imageSize.height;
  172. }
  173. }
  174. self.promptImageView.frame = CGRectMake(0, 0, imgViewWidth, imgViewHeight);
  175. contentWidth = self.promptImageView.ym_empty_size.width;
  176. contentHeight = self.promptImageView.ym_empty_maxY;
  177. }
  178. - (void)setupTitleLabel:(NSString *)titleStr{
  179. UIFont *font = self.titleLabFont.pointSize ? self.titleLabFont : kYMEmptyTitleLabFont;
  180. CGFloat fontSize = font.pointSize;
  181. UIColor *textColor = self.titleLabTextColor ? self.titleLabTextColor : kYMEmptyBlackColor;
  182. CGFloat titleMargin = self.titleLabMargin > 0 ? self.titleLabMargin : (contentHeight == 0 ?: subViweMargin);
  183. CGSize size = [self returnTextWidth:titleStr size:CGSizeMake(contentMaxWidth, fontSize + 5) font:font];
  184. CGFloat width = size.width;
  185. CGFloat height = size.height;
  186. self.titleLabel.frame = CGRectMake(0, contentHeight + titleMargin, width, height);
  187. self.titleLabel.font = font;
  188. self.titleLabel.text = titleStr;
  189. self.titleLabel.textColor = textColor;
  190. contentWidth = width > contentWidth ? width : contentWidth;
  191. contentHeight = self.titleLabel.ym_empty_maxY;
  192. }
  193. - (void)setupDetailLabel:(NSString *)detailStr{
  194. UIColor *textColor = self.detailLabTextColor ? self.detailLabTextColor : kYMEmptyGrayColor;
  195. UIFont *font = self.detailLabFont.pointSize ? self.detailLabFont : kYMEmptyDetailLabFont;
  196. CGFloat fontSize = font.pointSize;
  197. CGFloat maxLines = self.detailLabMaxLines > 0 ? self.detailLabMaxLines : 1;
  198. CGFloat detailMargin = self.detailLabMargin > 0 ? self.detailLabMargin : (contentHeight == 0 ?: subViweMargin);
  199. self.detailLabel.font = font;
  200. self.detailLabel.textColor = textColor;
  201. self.detailLabel.text = detailStr;
  202. CGFloat width = 0;
  203. CGFloat height = 0;
  204. //设置行高
  205. if(self.detailLabLineSpacing){
  206. CGFloat maxHeight = maxLines * (fontSize + 5) + (maxLines-1) * self.detailLabLineSpacing;
  207. NSDictionary *dic = [self sizeWithAttributedString:self.detailLabel.text font:font lineSpacing:self.detailLabLineSpacing maxSize:CGSizeMake(contentMaxWidth, maxHeight)];
  208. NSMutableAttributedString *attStr = dic[@"attributed"];
  209. NSValue *sizeValue = dic[@"size"];
  210. CGSize size = sizeValue.CGSizeValue;
  211. width = size.width;
  212. height = size.height;
  213. self.detailLabel.attributedText = attStr;
  214. }
  215. else{
  216. CGFloat maxHeight = maxLines * (fontSize + 5);
  217. CGSize size = [self returnTextWidth:detailStr size:CGSizeMake(contentMaxWidth, maxHeight) font:font];//计算得出label大小
  218. width = size.width;
  219. height = size.height;
  220. }
  221. self.detailLabel.frame = CGRectMake(0, contentHeight + detailMargin, width, height);
  222. contentWidth = width > contentWidth ? width : contentWidth;
  223. contentHeight = self.detailLabel.ym_empty_maxY;
  224. }
  225. - (void)setupActionBtn:(NSString *)btnTitle target:(id)target action:(SEL)action btnClickBlock:(YMActionTapBlock)btnClickBlock{
  226. UIFont *font = self.actionBtnFont.pointSize ? self.actionBtnFont : kYMEmptyActionBtnFont;
  227. CGFloat fontSize = font.pointSize;
  228. UIColor *titleColor = self.actionBtnTitleColor ?: kYMEmptyBlackColor;
  229. UIColor *backGColor = self.actionBtnBackGroundColor ?: [UIColor whiteColor];
  230. UIColor *borderColor = self.actionBtnBorderColor ?: [UIColor colorWithRed:0.8f green:0.8f blue:0.8f alpha:1.f];
  231. CGFloat borderWidth = self.actionBtnBorderWidth ?: 0;
  232. CGFloat cornerRadius = self.actionBtnCornerRadius ?: 0;
  233. CGFloat width = self.actionBtnWidth;
  234. CGFloat horiMargin = self.actionBtnHorizontalMargin;
  235. CGFloat height = self.actionBtnHeight;
  236. CGSize textSize = [self returnTextWidth:btnTitle size:CGSizeMake(contentMaxWidth, fontSize) font:font];//计算得出title文字内容大小
  237. if (height < textSize.height) {
  238. height = textSize.height + 4.f;
  239. }
  240. //按钮的宽
  241. CGFloat btnWidth = textSize.width;
  242. if (width) {
  243. btnWidth = width;
  244. } else if (horiMargin) {
  245. btnWidth = textSize.width + horiMargin * 2.f;
  246. }
  247. //按钮的高
  248. CGFloat btnHeight = height;
  249. btnWidth = btnWidth > contentMaxWidth ? contentMaxWidth : btnWidth;
  250. CGFloat btnMargin = self.actionBtnMargin > 0 ? self.actionBtnMargin : (contentHeight == 0 ?: subViweMargin);
  251. self.actionButton.frame = CGRectMake(0, contentHeight + btnMargin, btnWidth, btnHeight);
  252. [self.actionButton setTitle:btnTitle forState:UIControlStateNormal];
  253. self.actionButton.titleLabel.font = font;
  254. self.actionButton.backgroundColor = backGColor;
  255. if (self.actionBtnBackGroundGradientColors) [self addGradientWithView:self.actionButton gradientColors:self.actionBtnBackGroundGradientColors];
  256. [self.actionButton setTitleColor:titleColor forState:UIControlStateNormal];
  257. self.actionButton.layer.borderColor = borderColor.CGColor;
  258. self.actionButton.layer.borderWidth = borderWidth;
  259. self.actionButton.layer.cornerRadius = cornerRadius;
  260. //添加事件
  261. if (target && action) {
  262. [self.actionButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
  263. [self.actionButton addTarget:self action:@selector(actionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  264. }else if (btnClickBlock) {
  265. [self.actionButton addTarget:self action:@selector(actionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  266. }
  267. contentWidth = btnWidth > contentWidth ? btnWidth : contentWidth;
  268. contentHeight = self.actionButton.ym_empty_maxY;
  269. }
  270. #pragma mark - ------------------ Event Method ------------------
  271. - (void)actionBtnClick:(UIButton *)sender{
  272. if (self.btnClickBlock) {
  273. self.btnClickBlock();
  274. }
  275. }
  276. #pragma mark - ------------------ setter ------------------
  277. - (void)setEmptyViewIsCompleteCoverSuperView:(BOOL)emptyViewIsCompleteCoverSuperView{
  278. _emptyViewIsCompleteCoverSuperView = emptyViewIsCompleteCoverSuperView;
  279. if (emptyViewIsCompleteCoverSuperView) {
  280. if (!self.backgroundColor || [self.backgroundColor isEqual:[UIColor clearColor]]) {
  281. self.backgroundColor = kYMEmptyBackgroundColor;
  282. }
  283. [self setNeedsLayout];
  284. }else{
  285. self.backgroundColor = [UIColor clearColor];
  286. }
  287. }
  288. #pragma mark 内容物背景视图 相关
  289. - (void)setSubViewMargin:(CGFloat)subViewMargin{
  290. if (_subViewMargin != subViewMargin) {
  291. _subViewMargin = subViewMargin;
  292. [self reSetupSubviews];
  293. }
  294. }
  295. - (void)setTitleLabMargin:(CGFloat)titleLabMargin{
  296. if (_titleLabMargin != titleLabMargin) {
  297. _titleLabMargin = titleLabMargin;
  298. [self reSetupSubviews];
  299. }
  300. }
  301. - (void)setDetailLabMargin:(CGFloat)detailLabMargin{
  302. if (_detailLabMargin != detailLabMargin) {
  303. _detailLabMargin = detailLabMargin;
  304. [self reSetupSubviews];
  305. }
  306. }
  307. - (void)setActionBtnMargin:(CGFloat)actionBtnMargin{
  308. if (_actionBtnMargin != actionBtnMargin) {
  309. _actionBtnMargin = actionBtnMargin;
  310. [self reSetupSubviews];
  311. }
  312. }
  313. - (void)reSetupSubviews{
  314. if (_promptImageView || _titleLabel || _detailLabel || _actionButton || self.customView) {//此判断的意思只是确定self是否已加载完毕
  315. [self setupSubviews];
  316. }
  317. }
  318. - (void)setContentViewOffset:(CGFloat)contentViewOffset{
  319. if (_contentViewOffset != contentViewOffset) {
  320. _contentViewOffset = contentViewOffset;
  321. if (_promptImageView || _titleLabel || _detailLabel || _actionButton || self.customView) {
  322. self.ym_empty_centerY += self.contentViewOffset;
  323. }
  324. }
  325. }
  326. - (void)setContentViewY:(CGFloat)contentViewY{
  327. if (_contentViewY != contentViewY) {
  328. _contentViewY = contentViewY;
  329. if (_promptImageView || _titleLabel || _detailLabel || _actionButton || self.customView) {
  330. self.ym_empty_y = self.contentViewY;
  331. }
  332. }
  333. }
  334. #pragma mark 提示图Image 相关
  335. - (void)setImageSize:(CGSize)imageSize{
  336. if (_imageSize.width != imageSize.width || _imageSize.height != imageSize.height) {
  337. _imageSize = imageSize;
  338. if (_promptImageView) {
  339. [self setupSubviews];
  340. }
  341. }
  342. }
  343. #pragma mark 描述Label 相关
  344. - (void)setTitleLabFont:(UIFont *)titleLabFont{
  345. if (_titleLabFont != titleLabFont) {
  346. _titleLabFont = titleLabFont;
  347. if (_titleLabel) {
  348. [self setupSubviews];
  349. }
  350. }
  351. }
  352. - (void)setTitleLabTextColor:(UIColor *)titleLabTextColor{
  353. if (_titleLabTextColor != titleLabTextColor) {
  354. _titleLabTextColor = titleLabTextColor;
  355. if (_titleLabel) {
  356. _titleLabel.textColor = titleLabTextColor;
  357. }
  358. }
  359. }
  360. #pragma mark 详细描述Label 相关
  361. - (void)setDetailLabFont:(UIFont *)detailLabFont{
  362. if (_detailLabFont != detailLabFont) {
  363. _detailLabFont = detailLabFont;
  364. if (_detailLabel) {
  365. [self setupSubviews];
  366. }
  367. }
  368. }
  369. - (void)setDetailLabMaxLines:(NSInteger)detailLabMaxLines{
  370. if (_detailLabMaxLines != detailLabMaxLines) {
  371. _detailLabMaxLines = detailLabMaxLines;
  372. if (_detailLabel) {
  373. [self setupSubviews];
  374. }
  375. }
  376. }
  377. - (void)setDetailLabTextColor:(UIColor *)detailLabTextColor{
  378. if (_detailLabTextColor != detailLabTextColor) {
  379. _detailLabTextColor = detailLabTextColor;
  380. if (_detailLabel) {
  381. _detailLabel.textColor = detailLabTextColor;
  382. }
  383. }
  384. }
  385. - (void)setDetailLabLineSpacing:(NSInteger)detailLabLineSpacing{
  386. if (_detailLabLineSpacing != detailLabLineSpacing) {
  387. _detailLabLineSpacing = detailLabLineSpacing;
  388. if (_detailLabel) {
  389. [self setupSubviews];
  390. }
  391. }
  392. }
  393. #pragma mark Button 相关
  394. //////////大小位置相关-需要重新布局
  395. - (void)setActionBtnFont:(UIFont *)actionBtnFont{
  396. if (_actionBtnFont != actionBtnFont) {
  397. _actionBtnFont = actionBtnFont;
  398. if (_actionButton) {
  399. [self setupSubviews];
  400. }
  401. }
  402. }
  403. - (void)setActionBtnHeight:(CGFloat)actionBtnHeight{
  404. if (_actionBtnHeight != actionBtnHeight) {
  405. _actionBtnHeight = actionBtnHeight;
  406. if (_actionButton) {
  407. [self setupSubviews];
  408. }
  409. }
  410. }
  411. - (void)setActionBtnWidth:(CGFloat)actionBtnWidth{
  412. if (_actionBtnWidth != actionBtnWidth) {
  413. _actionBtnWidth = actionBtnWidth;
  414. if (_actionButton) {
  415. [self setupSubviews];
  416. }
  417. }
  418. }
  419. - (void)setActionBtnHorizontalMargin:(CGFloat)actionBtnHorizontalMargin{
  420. if (_actionBtnHorizontalMargin != actionBtnHorizontalMargin) {
  421. _actionBtnHorizontalMargin = actionBtnHorizontalMargin;
  422. if (_actionButton) {
  423. [self setupSubviews];
  424. }
  425. }
  426. }
  427. //////////其他相关-直接赋值
  428. - (void)setActionBtnCornerRadius:(CGFloat)actionBtnCornerRadius{
  429. if (_actionBtnCornerRadius != actionBtnCornerRadius) {
  430. _actionBtnCornerRadius = actionBtnCornerRadius;
  431. if (_actionButton) {
  432. _actionButton.layer.cornerRadius = actionBtnCornerRadius;
  433. }
  434. }
  435. }
  436. - (void)setActionBtnBorderWidth:(CGFloat)actionBtnBorderWidth{
  437. if (actionBtnBorderWidth != _actionBtnBorderWidth) {
  438. _actionBtnBorderWidth = actionBtnBorderWidth;
  439. if (_actionButton) {
  440. _actionButton.layer.borderWidth = actionBtnBorderWidth;
  441. }
  442. }
  443. }
  444. - (void)setActionBtnBorderColor:(UIColor *)actionBtnBorderColor{
  445. if (_actionBtnBorderColor != actionBtnBorderColor) {
  446. _actionBtnBorderColor = actionBtnBorderColor;
  447. if (_actionButton) {
  448. _actionButton.layer.borderColor = actionBtnBorderColor.CGColor;
  449. }
  450. }
  451. }
  452. - (void)setActionBtnTitleColor:(UIColor *)actionBtnTitleColor{
  453. if (_actionBtnTitleColor != actionBtnTitleColor) {
  454. _actionBtnTitleColor = actionBtnTitleColor;
  455. if (_actionButton) {
  456. [_actionButton setTitleColor:actionBtnTitleColor forState:UIControlStateNormal];
  457. }
  458. }
  459. }
  460. - (void)setActionBtnBackGroundColor:(UIColor *)actionBtnBackGroundColor{
  461. if (actionBtnBackGroundColor != _actionBtnBackGroundColor) {
  462. _actionBtnBackGroundColor = actionBtnBackGroundColor;
  463. if (_actionButton) {
  464. [_actionButton setBackgroundColor:actionBtnBackGroundColor];
  465. }
  466. }
  467. }
  468. - (void)setActionBtnBackGroundGradientColors:(NSArray<UIColor *> *)actionBtnBackGroundGradientColors
  469. {
  470. if (actionBtnBackGroundGradientColors.count >= 2) {
  471. _actionBtnBackGroundGradientColors = [actionBtnBackGroundGradientColors subarrayWithRange:NSMakeRange(0, 2)];
  472. if (_actionButton) {
  473. [self addGradientWithView:_actionButton gradientColors:_actionBtnBackGroundGradientColors];
  474. }
  475. }
  476. }
  477. #pragma mark - ------------------ getter ------------------
  478. - (UIImageView *)promptImageView{
  479. if (!_promptImageView) {
  480. _promptImageView = [[UIImageView alloc] init];
  481. _promptImageView.contentMode = UIViewContentModeScaleAspectFit;
  482. [self.contentView addSubview:_promptImageView];
  483. }
  484. return _promptImageView;
  485. }
  486. - (UILabel *)titleLabel{
  487. if (!_titleLabel) {
  488. _titleLabel = [[UILabel alloc] init];
  489. _titleLabel.textAlignment = NSTextAlignmentCenter;
  490. [self.contentView addSubview:_titleLabel];
  491. }
  492. return _titleLabel;
  493. }
  494. - (UILabel *)detailLabel{
  495. if (!_detailLabel) {
  496. _detailLabel = [[UILabel alloc] init];
  497. _detailLabel.textAlignment = NSTextAlignmentCenter;
  498. _detailLabel.numberOfLines = 0;
  499. [self.contentView addSubview:_detailLabel];
  500. }
  501. return _detailLabel;
  502. }
  503. - (UIButton *)actionButton{
  504. if (!_actionButton) {
  505. _actionButton = [[UIButton alloc] init];
  506. _actionButton.layer.masksToBounds = YES;
  507. [self.contentView addSubview:_actionButton];
  508. }
  509. return _actionButton;
  510. }
  511. #pragma mark - ------------------ Help Method ------------------
  512. - (CGSize)returnTextWidth:(NSString *)text size:(CGSize)size font:(UIFont *)font{
  513. CGSize textSize = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : font} context:nil].size;
  514. return textSize;
  515. }
  516. - (NSDictionary *)sizeWithAttributedString:(NSString *)string font:(UIFont *)font lineSpacing:(CGFloat)lineSpacing maxSize:(CGSize)maxSize{
  517. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  518. paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
  519. paragraphStyle.lineSpacing = lineSpacing; // 设置行间距
  520. paragraphStyle.alignment = NSTextAlignmentCenter;
  521. NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:string attributes:@{NSParagraphStyleAttributeName: paragraphStyle, NSFontAttributeName:font}];
  522. CGSize size = [attributedStr boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
  523. NSDictionary *dic = @{
  524. @"attributed":attributedStr,
  525. @"size": [NSValue valueWithCGSize:size]
  526. };
  527. return dic;
  528. }
  529. - (void)addGradientWithView:(UIView *)view gradientColors:(NSArray<UIColor *> *)gradientColors
  530. {
  531. [view setBackgroundColor:[UIColor clearColor]];
  532. NSArray *colors = @[(__bridge id)[gradientColors.firstObject CGColor],
  533. (__bridge id)[gradientColors.lastObject CGColor]];
  534. CAGradientLayer *layer = [CAGradientLayer layer];
  535. layer.colors = colors;
  536. layer.locations = @[@0.3, @0.5, @1.0];
  537. layer.startPoint = CGPointMake(0, 0);
  538. layer.endPoint = CGPointMake(1.0, 0);
  539. layer.frame = view.bounds;
  540. layer.masksToBounds = YES;
  541. layer.cornerRadius = view.frame.size.height * 0.5;
  542. CALayer *firstLayer = self.layer.sublayers.firstObject;
  543. if ([firstLayer isKindOfClass:[CAGradientLayer class]]) {
  544. [view.layer replaceSublayer:firstLayer with:layer];
  545. } else {
  546. [view.layer insertSublayer:layer atIndex:0];
  547. }
  548. }
  549. @end