简体   繁体   中英

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.

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);

您可以像上面那样获取UILable字体名称和字体大小,还可以使用其setValue方法更改滑块的值,例如波纹管...

[yourSlider setValue:yourFloatValue];

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