YJLAttributesLabel.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // YJLAttributesLabel.m
  3. // Asteria
  4. //
  5. // Created by 王猛 on 2024/1/5.
  6. //
  7. #import "YJLAttributesLabel.h"
  8. @interface YJLTextView : UITextView
  9. @end
  10. @implementation YJLTextView
  11. - (BOOL)canPerformAction:(SEL)action withSender:(id)sender
  12. {
  13. // 返回NO为禁用,YES为开启
  14. // 粘贴
  15. if (action == @selector(paste:)) return NO;
  16. // 剪切
  17. if (action == @selector(cut:)) return NO;
  18. // 复制
  19. if (action == @selector(copy:)) return NO;
  20. // 选择
  21. if (action == @selector(select:)) return NO;
  22. // 选中全部
  23. if (action == @selector(selectAll:)) return NO;
  24. // 删除
  25. if (action == @selector(delete:)) return NO;
  26. // 分享
  27. if (action == @selector(share)) return NO;
  28. return [super canPerformAction:action withSender:sender];
  29. }
  30. - (BOOL)canBecomeFirstResponder {
  31. return NO;
  32. }
  33. @end
  34. @interface YJLAttributesLabel()<UITextViewDelegate>
  35. @property (nonatomic , strong) YJLTextView *textView ;
  36. @property (nonatomic , strong) NSMutableArray *actionText ;
  37. @property (nonatomic , strong) NSMutableArray *actionRange ;
  38. @end
  39. @implementation YJLAttributesLabel
  40. - (void)setAttributesText: (NSMutableAttributedString *)text actionText: (NSMutableArray *)actionText actionRange:(NSMutableArray *)actionrange{
  41. self.textView.attributedText = text;
  42. self.actionText = actionText;
  43. self.actionRange = actionrange;
  44. }
  45. - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction {
  46. if ([self.actionRange containsObject:[NSString stringWithFormat:@"%ld",characterRange.location]]) {
  47. NSLog(@"%ld",characterRange.location);
  48. NSInteger index = [self.actionRange indexOfObject:[NSString stringWithFormat:@"%ld",characterRange.location]];
  49. self.YJLAttributesBlock(self.actionText[index]);
  50. return NO;
  51. }
  52. return YES;
  53. }
  54. - (instancetype)init {
  55. if (self == [super init]) {
  56. [self setupUI];
  57. [self configuration];
  58. }
  59. return self;
  60. }
  61. - (instancetype)initWithFrame:(CGRect)frame {
  62. if (self == [super initWithFrame:frame]) {
  63. [self setupUI];
  64. [self configuration];
  65. }
  66. return self;
  67. }
  68. - (void)configuration {
  69. self.userInteractionEnabled = YES;
  70. }
  71. - (void)setupUI {
  72. [self addSubview:self.textView];
  73. }
  74. - (void)layoutSubviews {
  75. self.textView.frame = self.bounds;
  76. }
  77. - (YJLTextView *)textView {
  78. if (_textView == nil) {
  79. _textView = [[YJLTextView alloc]init];
  80. _textView.backgroundColor = self.backgroundColor;
  81. _textView.textColor = self.textColor;
  82. self.textColor = [UIColor clearColor];
  83. _textView.font = self.font;
  84. _textView.scrollEnabled = NO;
  85. _textView.text = self.text;
  86. _textView.delegate = self;
  87. _textView.editable = NO;
  88. _textView.textAlignment = self.textAlignment;
  89. _textView.linkTextAttributes = @{NSForegroundColorAttributeName:ThemeColor};
  90. }
  91. return _textView;
  92. }
  93. @end