I try to change font color from white to black for UISegmentedControl
(for iOS 4.*)
UISegmentedControl *button = [[[UISegmentedControl alloc] in
Im using monotouch. Dont know why, but when View was pushed text color doesnt changed for me. for solve this im just add labels to segment control superview and then change their colours:
public static void SetColoredTittles(this UISegmentedControl s, string[] titles, UIColor selected, UIColor notSelected)
{
var segmentedLabels = new List<UILabel>();
float width = s.Frame.Width/titles.Length;
for (int i = 0; i < titles.Length; i++)
{
var frame = new RectangleF(s.Frame.X + i*width, s.Frame.Y, width,s.Frame.Height);
UILabel label = new UILabel(frame);
label.TextAlignment = UITextAlignment.Center;
label.BackgroundColor = UIColor.Clear;
label.Font = UIFont.BoldSystemFontOfSize(12f);
label.Text = titles[i];
s.Superview.AddSubview(label);
segmentedLabels.Add(label);
}
s.ValueChanged += delegate
{
TextColorChange(s,segmentedLabels, selected, notSelected);
};
TextColorChange(s,segmentedLabels, selected, notSelected);
}
static void TextColorChange(UISegmentedControl s, List<UILabel> segmentedLabels, UIColor selected, UIColor notSelected)
{
for (int i = 0; i < segmentedLabels.Count; i++)
{
if(i == s.SelectedSegment) segmentedLabels[i].TextColor = selected;
else segmentedLabels[i].TextColor = notSelected;
}
}
and then use it
segmented.SetColoredTittles(new string[] {
"text1",
"text2",
"text3"
}, UIColor.White,UIColor.DarkGray);
Below is the code to set the font to the bold face and point size:
UIFont *Boldfont = [UIFont boldSystemFontOfSize:12.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:Boldfont
forKey: NSFontAttributeName];
[segmentedControl setTitleTextAttributes:attributes
forState:UIControlStateNormal];
I hope it helps
Swift 5 Extension
extension UISegmentedControl {
func setTitleColor(_ color: UIColor, state: UIControl.State = .normal) {
var attributes = self.titleTextAttributes(for: state) ?? [:]
attributes[.foregroundColor] = color
self.setTitleTextAttributes(attributes, for: state)
}
func setTitleFont(_ font: UIFont, state: UIControl.State = .normal) {
var attributes = self.titleTextAttributes(for: state) ?? [:]
attributes[.font] = font
self.setTitleTextAttributes(attributes, for: state)
}
}
Just in case to help someone else that get there and is working using swift.
I will put the two possible ways of doing it. You can change the text attributes in the UIAppearance
or directly in the segmented your are working.
The first example setting the attributes in the appearance, this way you will customize all the segmented controls in your app:
let attributes = [ NSForegroundColorAttributeName : UIColor.grayColor(),
NSFontAttributeName : UIFont.systemFontOfSize(20)];
let attributesSelected = [ NSForegroundColorAttributeName : UIColor.blueColor(),
NSFontAttributeName : UIFont.systemFontOfSize(20)];
UISegmentedControl.appearance().setTitleTextAttributes(attributes, forState: UIControlState.Normal)
UISegmentedControl.appearance().setTitleTextAttributes(attributesSelected, forState: UIControlState.Selected)
The second example, directly in the segmented, will customize only this segmented:
let attributes = [ NSForegroundColorAttributeName : UIColor.grayColor(),
NSFontAttributeName : UIFont.systemFontOfSize(20)];
let attributesSelected = [ NSForegroundColorAttributeName : UIColor.blueColor(),
NSFontAttributeName : UIFont.systemFontOfSize(20)];
segmented.setTitleTextAttributes(attributes, forState: UIControlState.Normal)
segmented.setTitleTextAttributes(attributesSelected, forState: UIControlState.Selected)
In iOS 5.0 and later you can use the titleTextAttributes to customize UISegmentedControl
objects :
NSDictionary *segmentedControlTextAttributes = @{NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue" size:18.0], NSForegroundColorAttributeName:[UIColor whiteColor]};
[self.segmentedControl setTitleTextAttributes:segmentedControlTextAttributes forState:UIControlStateNormal];
[self.segmentedControl setTitleTextAttributes:segmentedControlTextAttributes forState:UIControlStateHighlighted];
[self.segmentedControl setTitleTextAttributes:segmentedControlTextAttributes forState:UIControlStateSelected];
Here I set the font to a custom font, font size, color for each state of the UISegmentedControl
.
You'll find every possible simple customizations in the Customizing Appearance section of the UISegmentedControl Class Reference.
for (UIView *v in [[[segment subviews] objectAtIndex:0] subviews]) {
if ([v isKindOfClass:[UILabel class]]) {
UILabel *label=(UILabel *)[v retain];
lable.textColor=[UIColor blackColor];
}
}
for iOS 3.0 and above