具有不同文本大小的ui段控件
uisegment control with diff text size
我一直在尝试将 UISegmentControl 自定义为如下所示:
首先我遍历 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;
}
}];
}];
这是结果:
更改附加到视图的标签属性有效:
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?
UISegmentedControl
不支持您的操作。 API 仅支持使用“setTitleTextAttributes:forState:”方法为所有片段的标题设置单一字体。
最终,分段控件将重置您在挖掘私有子视图后可能设置的任何属性。与 API 作斗争从来都不是一个好主意,深入研究一个视图的未记录的子视图也不是一个好主意。此类解决方案有时可能会奏效,但大多数都注定会在未来 iOS 更新可用时失效。
您唯一的选择是创建自己的自定义控件来执行您想要的操作,或者找到其他人创建的控件。
不要那样做。分段控件旨在按原样使用。它的内部视图层次结构是私有的,并且可能会在 OS 版本之间发生变化。通过进入控制装置内部并四处乱弄,所有的赌注都被取消了。即使你今天让它工作,任何未来的 OS 版本都可能让你崩溃。
如果您想自定义分段控件,请自己从头开始构建。这并不难。事实上,它可能比您尝试做的更容易,而且肯定更安全。
我一直在尝试将 UISegmentControl 自定义为如下所示:
首先我遍历 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;
}
}];
}];
这是结果:
更改附加到视图的标签属性有效:
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?
UISegmentedControl
不支持您的操作。 API 仅支持使用“setTitleTextAttributes:forState:”方法为所有片段的标题设置单一字体。
最终,分段控件将重置您在挖掘私有子视图后可能设置的任何属性。与 API 作斗争从来都不是一个好主意,深入研究一个视图的未记录的子视图也不是一个好主意。此类解决方案有时可能会奏效,但大多数都注定会在未来 iOS 更新可用时失效。
您唯一的选择是创建自己的自定义控件来执行您想要的操作,或者找到其他人创建的控件。
不要那样做。分段控件旨在按原样使用。它的内部视图层次结构是私有的,并且可能会在 OS 版本之间发生变化。通过进入控制装置内部并四处乱弄,所有的赌注都被取消了。即使你今天让它工作,任何未来的 OS 版本都可能让你崩溃。
如果您想自定义分段控件,请自己从头开始构建。这并不难。事实上,它可能比您尝试做的更容易,而且肯定更安全。