2016-09-22 70 views
1

我一直在試圖定義UISegmentControl看起來像這樣:uisegment控制與差異文字大小

enter image description here

通過UISegmentControl標籤

首先我環路和各設定爲多,但是當我嘗試改變標籤的文字屬性不會改變字體。我試圖用一個普通的UILabel這個屬性和它的作品,而不是內uisegment

[segmentedControl.subviews enumerateObjectsUsingBlock:^(UIView * obj, NSUInteger idx, BOOL *stop) { 
     [obj.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
      if ([obj isKindOfClass:[UILabel class]]) { 
       //Multiline 
       UILabel *_tempLabel = (UILabel *)obj; 
       [_tempLabel setNumberOfLines:0]; 

       NSMutableAttributedString *attString = 
          [[NSMutableAttributedString alloc] 
            initWithString: @"monkey goat"]; 

[attString addAttribute: NSForegroundColorAttributeName 
       value: [UIColor redColor] 
       range: NSMakeRange(0,6)]; 


[attString addAttribute: NSFontAttributeName 
       value: [UIFont fontWithName:@"Helvetica" size:15] 
       range: NSMakeRange(0,6)]; 

[attString addAttribute: NSFontAttributeName 
       value: [UIFont fontWithName:@"Didot" size:24] 
       range: NSMakeRange(7,4)]; 

_tempLabel.attributedText = attString; 
      } 
     }]; 
    }]; 

這是結果:連接查看作品

enter image description here

更改標籤屬性:

NSMutableAttributedString *attString = 
          [[NSMutableAttributedString alloc] 
            initWithString: @"monkey goat"]; 

[attString addAttribute: NSForegroundColorAttributeName 
       value: [UIColor redColor] 
       range: NSMakeRange(0,6)]; 


[attString addAttribute: NSFontAttributeName 
       value: [UIFont fontWithName:@"Helvetica" size:15] 
       range: NSMakeRange(0,6)]; 

[attString addAttribute: NSFontAttributeName 
       value: [UIFont fontWithName:@"Didot" size:24] 
       range: NSMakeRange(7,4)]; 

self.label.attributedText = attString; 

SO改變標籤屬性的鏈接:Different font size in the same label?

回答

1

UISegmentedControl不支持您要做的事情。 API僅支持使用'setTitleTextAttributes:forState:`方法爲所有分段標題設置單個字體。

最終,分段控件將重新設置通過私人子視圖挖掘後可能設置的任何屬性。打一個API不是一個好主意,也不是一個好主意來挖掘一個視圖的無證子視圖。此類解決方案有時可能會有效,但大多數解決方案在未來的iOS更新可用時都會中斷。

你唯一的選擇是創建你自己的自定義控件,它可以做你想做的或找到別人創建的控件。

1

不要這樣做。分段控制旨在按原樣使用。它的內部視圖層次結構是私有的,並且可能會在操作系統版本之間發生變化通過進入控制區並在附近進行投注,所有投注都關閉。即使你今天得到它的工作,任何未來的操作系統版本都可能會打破你。

如果你想自定義一個分段控制從頭開始自己構建它。這並不難。事實上,它可能比你想要做的更容易,而且肯定更安全。