Search code examples
iphoneiosuilabeluislider

uislider value changes when I click on programmatically created uilabel


I have a uislider (*fontSizeSlider) which changes programmatically created uilabel's text size. When I have like 2 to 5 programmatically created labels in my view with different text size, I want the UISlider position to be changed to its corresponding value(1-50 is the min and max size of uislider) for each label when touched(touchesbegan).

Such that, if label1 textsize is 10 and label2 textsize is 20 and label5 textsize is 50(Note: the text size was change using the uislider). When I click(touch) label1 or label2 or label5, I want the uislider to show the corresponding values.(What i mean is that the uislider bar should move to 10 if label1 is selected and to 20 when label2 is selected and so on.)

Here is my sample code which i tried on touchesbegan,

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
 touch=[touches anyObject];
 CGPoint fontSliderLocation = [touch locationInView:fontSizeSlider];
        NSLog(@"Location of x %f and Location of y %f", fontSliderLocation.x, fontSliderLocation.y);

This is not the full code, I just put it here to make some sense. Using the above code I'm able to get the x and y location of the touched label. But, how do i get the text size of the label and be able to change the uislider value when i click/touch on any of the labels.


Solution

  • how do i get the text size of the label and be able to change the uislider value when i click/touch on any of the labels.

    You can get the fontsize by this:

    NSString *fontName=self.label.font.fontName;
    CGFloat fontSize=self.label.font.pointSize;
    

    And on this delegate :

    -(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event;
        UITouch *touch=[touches <object>];
        if(touch.view.tag == <your Tag>){
           slider.value=<the value you want>;
        }
        else...
     }
    

    Edit: For knowing the color use:

    UIColor *color=self.textField.textColor;
    CGFloat r,g,b,a;
    [color getRed:&r green:&g blue: &b alpha: &a];
    NSLog(@"%f, %f, %f",r, g, b);