Created
July 26, 2025 12:14
-
-
Save pookjw/3b62649508b8c4c17556621d4922795b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "ViewController.h" | |
| #include <objc/message.h> | |
| #include <objc/runtime.h> | |
| @interface MyLabel : UILabel | |
| @end | |
| @implementation MyLabel | |
| - (void)willMoveToWindow:(UIWindow *)newWindow { | |
| [super willMoveToWindow:newWindow]; | |
| if (newWindow == nil) { | |
| if (self.window != nil) { | |
| [NSNotificationCenter.defaultCenter removeObserver:self]; | |
| } | |
| } else { | |
| if (self.window == nil) { | |
| [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(didZoom:) name:@"UITextSelectionDidZoom" object:nil]; | |
| } | |
| } | |
| } | |
| - (void)didZoom:(NSNotification *)notification { | |
| UIScrollView * _Nullable scollView = ((id (*)(id, SEL))objc_msgSend)(self, sel_registerName("_scroller")); | |
| if (scollView == nil) return; | |
| if (![scollView isEqual:notification.object]) return; | |
| [CATransaction setDisableActions:YES]; | |
| self.contentScaleFactor = scollView.zoomScale; | |
| } | |
| @end | |
| @interface ViewController () <UIScrollViewDelegate> { | |
| MyLabel *_label; | |
| } | |
| @end | |
| @implementation ViewController | |
| - (void)dealloc { | |
| [_label release]; | |
| [super dealloc]; | |
| } | |
| - (void)viewDidLoad { | |
| [super viewDidLoad]; | |
| MyLabel *label = [[MyLabel alloc] initWithFrame:self.view.bounds]; | |
| label.text = @"A"; | |
| label.font = [UIFont systemFontOfSize:100.]; | |
| [label sizeToFit]; | |
| [_label release]; | |
| _label = label; | |
| UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds]; | |
| scrollView.delegate = self; | |
| scrollView.maximumZoomScale = 100.; | |
| scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; | |
| scrollView.contentSize = label.bounds.size; | |
| [self.view addSubview:scrollView]; | |
| [scrollView addSubview:label]; | |
| [scrollView release]; | |
| } | |
| - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { | |
| return _label; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment