2012-01-27 71 views
45

的字體顏色我嘗試從白色更改字體顏色爲黑色UISegmentedControl(適用於iOS 4 *)如何改變UISegmentedControl

UISegmentedControl *button = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:itemTitle, nil]] autorelease]; 
button.momentary = YES; 
button.segmentedControlStyle = UISegmentedControlStyleBar; 
button.tintColor = [UIColor redColor];  
for (id segment in [button subviews]) { 
    for (id label in [segment subviews]) { 
     if ([label isKindOfClass:[UILabel class]]) { 
      UILabel *titleLabel = (UILabel *) label; 
      [titleLabel setTextColor:[UIColor blackColor]]; 
     } 
    } 
} 
UIBarButtonItem *barButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease]; 

但文字顏色不改變。我應該怎樣改變UISegmentedControl的文字顏色?

回答

28

下面是字體設置爲粗體和大小代碼:

UIFont *Boldfont = [UIFont boldSystemFontOfSize:12.0f]; 
NSDictionary *attributes = [NSDictionary dictionaryWithObject:Boldfont 
                 forKey: NSFontAttributeName]; 
[segmentedControl setTitleTextAttributes:attributes 
           forState:UIControlStateNormal]; 

我希望它能幫助

+4

注意:此答案需要iOS 5. – Costique 2012-01-31 19:14:42

+1

此答案不會更改字體顏色作爲主題發起者請求的字體大小。 pbibergal的答案更完整。 – 2012-12-19 16:09:15

+0

這可行,但UITextAttributeFont的警告即將推出。而是使用「NSFontAttributeName」。所有感謝@johngraham從http://stackoverflow.com/questions/2280391/change-font-size-of-uisegmentedcontrol – mavericks 2015-09-22 10:29:21

7
for (UIView *v in [[[segment subviews] objectAtIndex:0] subviews]) { 
    if ([v isKindOfClass:[UILabel class]]) { 
     UILabel *label=(UILabel *)[v retain]; 
     lable.textColor=[UIColor blackColor]; 
    } 
} 

適用於iOS 3.0及以上

+5

警告:你不應該保留標籤。 – Costique 2012-02-19 11:01:46

+0

不適用於iOS 6 – CommaToast 2013-10-20 06:51:44

+0

也不適用於iOS 10 – 2017-08-15 11:35:46

1

進出口使用的MonoTouch 。 不知道爲什麼,但當查看被推動文字顏色不會改變我。 爲解決這個即時通訊只是標籤添加到分段控制上海華盈,然後改變自己的顏色:

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

,然後用它

segmented.SetColoredTittles(new string[] { 
      "text1", 
      "text2", 
      "text3" 
     }, UIColor.White,UIColor.DarkGray); 
107

在iOS系統6.0和它上面很簡單:

NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: 
          [UIFont boldSystemFontOfSize:17], NSFontAttributeName, 
          [UIColor blackColor], NSForegroundColorAttributeName, 
          nil]; 
[_segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal]; 
NSDictionary *highlightedAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName]; 
[_segmentedControl setTitleTextAttributes:highlightedAttributes forState:UIControlStateSelected]; 
+0

有關更改字體類型的任何想法? – 2013-04-24 05:57:32

+0

'[UIFont fontWithName:@「HelveticaNeue」size:17.0f]' – pbibergal 2013-04-25 08:34:50

+0

任何想法可能會導致這不起作用?作爲一個實驗,我將文本顏色設置爲綠色,但一切仍然是默認值(淺灰色背景上的灰色文本或藍色背景上的白色文本(如果突出顯示),即iOS6,iOS7默認爲白色/清晰。 – Olie 2014-03-18 16:28:58

57

如果您需要更改iOS 7中高亮顯示細分的文字顏色,請使用下面的解決方案(花了我一段時間才找到,但是thanks to this post):

Objective-C的

[[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]} forState:UIControlStateSelected]; 

斯威夫特

let titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()] 
    UISegmentedControl.appearance().setTitleTextAttributes(titleTextAttributes, forState: .Selected) 
+0

太好了。您可以按照詳細信息在http://datacalculation.blogspot.in/2014/10/how-to-change-uisegmentedcontrol-text.html – 2014-10-07 04:21:50

+2

我編輯你的好答案添加swift代碼 – 2016-04-29 07:50:25

+0

這個答案終於幫助我完成對我的應用程序的選擇按鈕的要求,以前包括一種保持在超級用戶界面中的分段控制的方式,使邊界正方形.... – 2016-05-04 06:13:02

2

在iOS系統5.0和更高版本可以使用titleTextAttributes定製UISegmentedControl對象:

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

這裏我設置fon t爲自定義字體,字體大小,顏色爲UISegmentedControl的每個狀態。

您會在UISegmentedControl類參考的Customizing Appearance部分找到所有可能的簡單自定義。

4

只是爲了幫助其他人到達那裏並使用swift工作。

我會把兩種可能的方式來做到這一點。您可以更改UIAppearance中的文本屬性,也可以直接在您正在工作的分段中使用。

第一個例子設置在外觀屬性,這樣您將定製應用中的所有分段控制:

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) 

第二個例子,直接在分段,將定製僅此細分:

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

SWIFT 3.2:

let attributes = [ 
          NSFontAttributeName : bigTextFont, 
          NSForegroundColorAttributeName : UIColor.blue, 
          ]   
segmentedControl?.setTitleTextAttributes(attributes, for: .normal) 

注:我f您使用自定義背景色,你會看到在角落毛刺(顏色將填補外段..),所以使用這些線將它夾:

segmentedControl!.layer.cornerRadius = 4.0 
segmentedControl!.clipsToBounds = true 
1

斯威夫特4代碼來設置這兩個州的字體顏色黑色

let titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black] 
    segmentedControl.setTitleTextAttributes(titleTextAttributes, for: .normal) 
    segmentedControl.setTitleTextAttributes(titleTextAttributes, for: .selected) 
相關問題