简体   繁体   中英

How to display fixed size text on UIView using pinchgesturerecognizer?

I have one view(myView) and some labels are created on myView because of display text on myView. I applied pinchgesturerecognizer on myView. It's worked very well. What my problem is when i pinch on myView the size of text of labels will be changed either increase or decrease depend on pinching. If you pinch (zoom in) myView, the sizes of text of labels are very small so we can't see the text. If you pinch (zoom out) myView, the sizes of text of labels are big.

So i would like to fix size of text of labels whatever you zoom in or zoom out myView. I have tried but i did not get answer. I also use setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleWidth setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleWidth but i didn't get it. I don't know how to use it. Please help me. Thanks in advance.

My Code is:

In ViewController.m ,

   - (void)viewDidLoad
    {
        MyView *myView = [[MyView  alloc] initWithFrame:CGRectMake(0, 100, 200, 100)];
    [self.view addSubview:myView];
        [super viewDidLoad];
    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scalePiece:)];
        [pinchGesture setDelegate:self];
        [myView addGestureRecognizer:pinchGesture];
        [pinchGesture release];
    }

- (void)scalePiece:(UIPinchGestureRecognizer *)gestureRecognizer
{

    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
        [gestureRecognizer view].transform = CGAffineTransformScale([[gestureRecognizer view] transform], [gestureRecognizer scale], [gestureRecognizer scale]);
           //[gestureRecognizer setScale:1];


    }
}

In MyView.m ,

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 35, 20)];
label1.font = [UIFont fontWithName:@"Helvetica-Bold" size:10];
label1.text = @"label1";
[self addSubview:label1];

label2 = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 35, 20)];
label2.font = [UIFont fontWithName:@"Helvetica-Bold" size:10];
label2.text = @"label2";
[self addSubview:label2];



label3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, 35, 20)];
label3.font = [UIFont fontWithName:@"Helvetica-Bold" size:10];
label3.text = @"label1";
[self addSubview:label3];

label4 = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 35, 20)];
label4.font = [UIFont fontWithName:@"Helvetica-Bold" size:10];
label4.text = @"label4";
[self addSubview:label4];
    }
    return self;
}

I would like to fix the texts of label1, label2,label3, label4.

It sounds like you are trying to zoom in on some items but not on others. There are two ways to approach this. Neither is very simple. You could break out the labels to their own view and manually reposition them after a pinch gesture on the other view. Or you could leave them in this view and manually rescale and position them after a pinch. Either way you'll have to do some math.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM