简体   繁体   English

区分文字大小的uisegment控件

[英]uisegment control with diff text size

I've been trying to customize UISegmentControl to look like this: 我一直试图自定义UISegmentControl看起来像这样:

在此处输入图片说明

First I loop through the labels in UISegmentControl and set each to multiline, but when I try to alter the label text attributes it doesn't change the font. 首先,我遍历UISegmentControl中的标签并将每个标签设置为多行,但是当我尝试更改标签文本属性时,它不会更改字体。 I tried using this attribute on a normal UILabel and it works, but not within uisegment 我尝试在普通的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;
            }
        }];
    }];

This is the result: 结果如下:

在此处输入图片说明

Changing label attribute attached to view works: 更改附加到视图作品的标签属性:

  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 links to change label attribute: Different font size in the same label? SO链接以更改标签属性: 同一标签中的字体大小是否不同?

UISegmentedControl does not support what you are trying to do. UISegmentedControl不支持您要执行的操作。 The API only supports setting a single font for all of the segment's titles by using the 'setTitleTextAttributes:forState:` method. API仅支持通过使用'setTitleTextAttributes:forState:`方法为所有细分受众群的标题设置一种字体。

Ultimately, the segmented control will reset whatever attributes you might set after digging through the private subviews. 最终,分段控件将在您浏览私有子视图后重置您可能设置的任何属性。 It'a never a good idea to fight an API nor is it ever a good idea to dig into a view's undocumented subviews. 对抗API从来不是一个好主意,而深入研究视图的未记录子视图也不是一个好主意。 Such solutions may work at times but most are doomed to break when a future iOS update is available. 这样的解决方案有时可能会起作用,但是大多数注定要在将来的iOS更新可用时中断。

Your only option is to create your own custom control that does what you want or find one created by someone else. 您唯一的选择是创建自己的自定义控件,该控件可以执行所需的操作或找到其他人创建的控件。

Don't do that. 不要那样做 A segmented control is intended to be used as-is. 分段控件应原样使用。 It's internal view hierarchy is private, and subject to change between OS versions. 它的内部视图层次结构是私有的,并且在OS版本之间可能会发生变化。 By reaching inside the control and mucking around, all bets are off. 通过进入控件内部并四处游荡,所有赌注都消失了。 Even if you get it working today, any future OS version could break you. 即使您现在就可以使用它,任何将来的OS版本都可能使您失望。

If you want to customize a segmented control build it yourself from scratch. 如果要自定义分段控件,请自己重新构建。 It isn't that hard. 没那么难。 In fact it's probably gonna be easier than what you're trying to do, and certainly much safer. 实际上,它可能会比您尝试做的要容易,并且肯定会更安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM