Label.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // Label.m
  3. //
  4. // Created by shengcui on 16/9/22.
  5. // Copyright © 2018年 Tencent. All rights reserved.
  6. //
  7. #import "Label.h"
  8. #import <objc/runtime.h>
  9. @implementation Label
  10. - (id)initWithFrame:(CGRect)frame{
  11. self = [super initWithFrame:frame];
  12. if (self) {
  13. self.edgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
  14. }
  15. return self;
  16. }
  17. - (void)drawTextInRect:(CGRect)rect {
  18. [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.edgeInsets)];
  19. }
  20. - (CGSize)intrinsicContentSize
  21. {
  22. CGSize size = [super intrinsicContentSize];
  23. size.width += self.edgeInsets.left + self.edgeInsets.right;
  24. size.height += self.edgeInsets.top + self.edgeInsets.bottom;
  25. return size;
  26. }
  27. - (CGSize)sizeThatFits:(CGSize)size {
  28. CGSize retSize = [super sizeThatFits:size];
  29. retSize.width += self.edgeInsets.left + self.edgeInsets.right;
  30. retSize.height += self.edgeInsets.top + self.edgeInsets.bottom;
  31. return CGSizeMake(ceilf(retSize.width), ceilf(retSize.height));
  32. }
  33. @end
  34. @interface UILabel (Copyable)
  35. @end
  36. static BOOL yes(id self, SEL cmd) {
  37. return YES;
  38. }
  39. @implementation UILabel (Copyable)
  40. + (void)load {
  41. Class clz = UILabel.class;
  42. unsigned int count = 0;
  43. Method *methods = class_copyMethodList(clz, &count);
  44. unsigned int canPerformActionIndex = -1;
  45. unsigned int canBecomeFirstResopnderIndex = -1;
  46. for (unsigned int i = 0; i < count; ++i) {
  47. SEL methodName = method_getName(methods[i]);
  48. if (methodName == @selector(canPerformAction:withSender:)) {
  49. canPerformActionIndex = i;
  50. NSLog(@"found");
  51. } else if (methodName == @selector(canBecomeFirstResponder)) {
  52. canBecomeFirstResopnderIndex = i;
  53. }
  54. }
  55. if (canPerformActionIndex > -1) {
  56. Method m0 = class_getInstanceMethod(self.class, @selector(canPerformAction:withSender:));
  57. Method m1 = class_getInstanceMethod(self.class, @selector(_canPerformAction:withSender:));
  58. method_exchangeImplementations(m0, m1);
  59. } else {
  60. Method method = class_getInstanceMethod(clz, @selector(_canPerformAction:withSender:));
  61. class_addMethod(clz, @selector(canPerformAction:withSender:), method_getImplementation(method), "B@::@");
  62. }
  63. if (canBecomeFirstResopnderIndex > -1) {
  64. class_replaceMethod(clz, @selector(canBecomeFirstResponder), (IMP)yes, "B@:");
  65. } else {
  66. class_addMethod(clz, @selector(canBecomeFirstResponder), (IMP)yes, "B@:");
  67. }
  68. free(methods);
  69. }
  70. - (BOOL)_canPerformAction:(SEL)action withSender:(id)sender
  71. {
  72. if (![self respondsToSelector:@selector(text)]) {
  73. return NO;
  74. }
  75. return self.text.length > 0 && action == @selector(copy:);
  76. }
  77. - (void)copy:(id)sender {
  78. [[UIPasteboard generalPasteboard] setString:self.text];
  79. }
  80. @end