I want to add a label to the slider's thumb which should show the value of the slider and changes too when thumbs is being dragged towards right side. Is it possible??
Any comments or suggestion would be appreciated.
I grab the thumb image from the slider (UIImageView) and add my label to it. Nice and clean.
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;
Then you change the label.text whenever you need to.
Note: the subview order of UISlider could change in the future, however it's unlikely that the thumb would no longer be the topmost view, as it will always be the main point of interaction in a slider.
Swift 3 -- More detailed example (link your slider in 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 = "!!"
}
}
}
You could do something similar to this example which draws text directly to your thumb image. It's a rough example so you will need to change it to make sense for your project.
- (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;
}
A simple implementation of a custom class in Swift 3.2. This is working nicely for me.
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
}
}
I hope it helps :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With