这是可能的标签添加到UISlider的拇指图像?

我想添加一个标签到滑块的拇指,它应该显示滑块的值,当拇指被拖向右侧时也会改变。 可能吗??

任何意见或build议,将不胜感激。

你可以做一些类似于这个例子,直接绘制文本到你的拇指图像。 这是一个粗略的例子,所以你需要改变它来使你的项目有意义。

- (IBAction)sliderValueChanged:(id)sender { UISlider *aSlider = (UISlider *)sender; NSString *strForThumbImage = [NSString stringWithFormat:@"%.0f", aSlider.value * 100] UIImage *thumbImage = [self addText:self.thumbImage text:strForThumbImage]; [aSlider setThumbImage:thumbImage forState:aSlider.state]; } //Add text to UIImage -(UIImage *)addText:(UIImage *)img text:(NSString *)text1{ int w = img.size.width; int h = img.size.height; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate( NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst); CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage); char* text= (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding]; CGContextSelectFont(context, "Arial",12, kCGEncodingMacRoman); CGContextSetTextDrawingMode(context, kCGTextFill); CGContextSetRGBFillColor(context, 0, 0, 0, 1); CGContextShowTextAtPoint(context,3,8,text, strlen(text)); CGImageRef imgCombined = CGBitmapContextCreateImage(context); CGContextRelease(context); CGColorSpaceRelease(colorSpace); UIImage *retImage = [UIImage imageWithCGImage:imgCombined]; CGImageRelease(imgCombined); return retImage; } 

我从滑块(UIImageView)抓住拇指图像,并添加我的标签。 好,干净。

 UIImageView *handleView = [slider.subviews lastObject]; UILabel *label = [[UILabel alloc] initWithFrame:handleView.bounds]; label.backgroundColor = [UIColor clearColor]; label.textAlignment = NSTextAlignmentCenter; [handleView addSubview:label]; self.sliderLabel = label; 

然后,只要需要,就可以更改label.text。

注意:UISlider的子视图顺序可能会在将来发生变化,但是拇指不可能不再是最顶层的视图,因为它总是滑块中的主要交互点。

Swift 3 – 更详细的例子(在IB中链接你的滑块)

 import UIKit class ViewController: UIViewController { @IBOutlet weak var slider: UISlider! var sliderLabel: UILabel? override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) if let handleView = slider.subviews.last as? UIImageView { let label = UILabel(frame: handleView.bounds) label.backgroundColor = .clear label.textAlignment = .center handleView.addSubview(label) self.sliderLabel = label //set label font, size, color, etc. label.text = "!!" } } } 

在Interface Builder中添加一个UISlider和一个UILabel 。 创buildIBOutlets以在代码中访问它们,并添加IBAction来响应滑块值的更改。

然后在你的代码中写入:

 - (void)viewDidLoad { [super viewDidLoad]; [self.slider addSubview:self.label]; [self valueChanged:self.slider]; } - (IBAction)valueChanged:(id)sender { self.label.center = CGPointMake(self.slider.value*self.slider.bounds.size.width, 40); self.label.text = [NSString stringWithFormat:@"%0.2f", self.slider.value]; } 

编辑:要创build代码滑块和标签添加此:

 -(void)loadView { [super loadView]; self.slider = [[[UISlider alloc] initWithFrame:CGRectMake(50, 150, 200, 30)] autorelease]; self.label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 30)] autorelease]; [self.label setBackgroundColor:[UIColor clearColor]]; [self.slider addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:self.slider]; } 

您可以通过-thumbRectForBounds:trackRect:value:]访问滑块的缩略图视图的矩形,因此如果添加UILabel子视图,则可以使用此返回的矩形在-layoutSubviews其相对于缩略图图像alignment。

但是,如果您想使用Autolayout将标签与拇指图像alignment,则需要直接访问拇指的UIImageView 。 我并不热衷于对UIKit工具包组件的子视图进行深入探讨,但我认为您可以使用公共API的详细信息以面向未来的方式search图像视图:

- (UIImageView*) thumbImageView { __block UIImageView *imageView = nil; [self.subviews enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(UIImageView *candidate, NSUInteger idx, BOOL *stop) { if ([candidate isKindOfClass:[UIImageView class]] && candidate.image == [self thumbImageForState:UIControlStateNormal]) { imageView = candidate; *stop = YES; } }]; return imageView; }

该API提供了为拇指设置UIImage的function,因此您可以将其用作testing,以确保您没有获取其他图像视图。 如果你自己明确地设置了一个拇指图像,这更安全(因为在将来可能改变滑块来绘制拇指图像不是不可能的)。 此外,拇指图像视图目前是最后一个子视图(不保证前进),所以我在这里做了一个反向枚举,希望尽可能快地得到它,而不做假设其索引。

注意:我不得不懒洋洋地制作我的拇指标签,因为UISlider的子视图似乎是这样的。

 - (IBAction)valueChangedSlider:(id)sender { handleView = [_slider.subviews lastObject]; label = [[UILabel alloc] initWithFrame:handleView.bounds]; label = (UILabel*)[handleView viewWithTag:1000]; if (label==nil) { label = [[UILabel alloc] initWithFrame:handleView.bounds]; label.tag = 1000; [label setFont:[UIFont systemFontOfSize:12]]; label.textColor = [UIColor redColor]; label.backgroundColor = [UIColor clearColor]; label.textAlignment = NSTextAlignmentCenter; [handleView addSubview:label]; } label.text = [NSString stringWithFormat:@"%0.2f", self.slider.value]; } 

使这个很好,容易..首先这是最终的结果

在这里输入图像说明

现在,代码:

 UILabel *sl = [UILabel new]; [v addSubview:sl]; [sl constraintHeightEqualTo:12 widthTo:26]; UIImageView *handleView = [slider.subviews lastObject]; [self.view constraintVerticalSpacingSubviewsFromBottomView:s toTopView:sl constant:10]; [self.view constraintCenterXOfView:sl equalToView:handleView]; [sl setText:@"100%"]; [sl setFont:[UIFont systemFontOfSize:10]]; self.sliderLabel = sl; 

上面的方法是从我使用的NSLayoutConstraint类中使用的(即将推出)

 - (id)constraintHeightEqualTo:(CGFloat)height widthTo:(CGFloat)width { [self setTranslatesAutoresizingMaskIntoConstraints:NO]; [self constraintHeightEqualTo:height]; [self constraintWidthEqualTo:width]; return self; } - (id)constraintVerticalSpacingSubviewsFromBottomView:(UIView *)fromView toTopView:(UIView *)toView constant:(CGFloat)constant { NSLayoutConstraint *cn = [NSLayoutConstraint constraintWithItem:fromView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:toView attribute:NSLayoutAttributeBottom multiplier:1 constant:constant]; [self addConstraint:cn]; return self; } - (id)constraintCenterXOfView:(UIView *)fromView equalToView:(UIView *)toView constant:(CGFloat)constant { NSLayoutConstraint *cn = [NSLayoutConstraint constraintWithItem:fromView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:toView attribute:NSLayoutAttributeCenterX multiplier:1 constant:constant]; [self addConstraint:cn]; return self; } 

Swift 3.2中一个简单的自定义类的实现。 这对我来说很好。

 class ThumbTextSlider: UISlider { var thumbTextLabel: UILabel = UILabel() private var thumbFrame: CGRect { return thumbRect(forBounds: bounds, trackRect: trackRect(forBounds: bounds), value: value) } override func layoutSubviews() { super.layoutSubviews() thumbTextLabel.frame = thumbFrame thumbTextLabel.text = Double(value).roundTo(places: 1).description } override func awakeFromNib() { super.awakeFromNib() addSubview(thumbTextLabel) thumbTextLabel.textAlignment = .center thumbTextLabel.layer.zPosition = layer.zPosition + 1 } } 

我希望它有助于:)