【问题标题】:uislider value changes when I click on programmatically created uilabel当我单击以编程方式创建的 uilabel 时,uislider 值会发生变化
【发布时间】:2013-04-03 06:42:53
【问题描述】:

我有一个 uislider (*fontSizeSlider),它以编程方式更改创建的 uilabel 的文本大小。当我在视图中有 2 到 5 个以编程方式创建的具有不同文本大小的标签时,我希望 UISlider 位置在触摸时更改为每个标签的相应值(1-50 是 uislider 的最小和最大大小)(touchesbegan )。

这样,如果 label1 textsize 为 10,label2 textsize 为 20,label5 textsize 为 50(注意:使用 uislider 更改文本大小)。当我单击(触摸)标签1或标签2或标签5时,我希望UISLIDER显示相应的值。(我的意思是,如果选择Label1并且在选择Label2时,UISlider栏应该移动到10。 )

这是我在 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);

这不是完整的代码,我只是把它放在这里有点意思。使用上面的代码,我可以获得触摸标签的 x 和 y 位置。但是,当我单击/触摸任何标签时,如何获取标签的文本大小并能够更改 uislider 值。

【问题讨论】:

    标签: iphone ios uilabel uislider


    【解决方案1】:

    如何获取标签的文本大小并能够更改 当我单击/触摸任何标签时的 uislider 值。

    您可以通过以下方式获得 fontsize

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

    在这个委托上:

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

    编辑:了解颜色用途:

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

    【讨论】:

    • 谢谢,这和我想要的完全一样。另一个快速的问题,有没有办法获取标签颜色并将其分配给 UiColorSlider?
    • 我试过了, const CGFloat *rgba = CGColorGetComponents([_label textColor].CGColor); r=rgba[0]; g=rgba[1]; //绿色总是返回1 b=rgba[2];红色和蓝色从标签返回分配给滑块的颜色,但绿色总是返回 1。
    • 然后我没有答案,我从来都不是 ios 开发人员,我也不是。在我的 XCodes 中它说的是 ios6.1 左右...... :(
    • Np。感谢您花时间帮我解答。
    • 我可以在我的 XCode4.6 中使用你的 CGColor 等吗?
    【解决方案2】:

    您可以像上面一样获取UILable 的字体名称和字体大小,还可以使用下面的setValue 方法更改滑块的值...

    [yourSlider setValue:yourFloatValue];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-22
      • 2012-03-26
      • 2012-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多