如何调整文本(字体)的大小以适应 UISegmentedControl 的 UISegment?
How to resize text (font) to fit in UISegment of UISegmentedControl?
有没有什么方法可以减小适合 UISegmentedControl
的单个段的字体大小?
尝试过很多类似的东西,
[[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] adjustsFontSizeToFitWidth];
[[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] setMinimumScaleFactor:0.5];
和
NSArray *arr = segment.subviews; // segment is UISegmentedControl object
for (int i = 0; i < arr.count; i++) {
UIView *aSegment = [arr objectAtIndex:i];
for (UILabel *label in aSegment.subviews) {
if ([label isKindOfClass:[UILabel class]]) {
UILabel *myLabel = (UILabel *)label;
[myLabel setNumberOfLines:0];
label.numberOfLines = 0;
label.adjustsFontSizeToFitWidth = YES;
label.minimumScaleFactor = 0.5;
}
}
}
可以设置段标签的行数,如
[[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] setNumberOfLines:0];
可以根据内容设置单段大小,
segment.apportionsSegmentWidthsByContent = YES;
但在这种情况下每个段的大小都不同。
我想保持每个段的大小相同,并希望减小适合 UISegmentLabel (label)
或 UISegmentedControl
的字体大小,例如 minimumscalefactor
或 minimumfontsize
或adjustsFontSizeToFitWidth
。当包含在 UISegmentedControl
.
中时,这些属性不适用于标签
如果有人能帮助实现这一点,我们将不胜感激!!
提前致谢!!
试试这个,我希望这会对你有所帮助,你会明白这是如何工作的-
我有一个 UISegmentedControl
即 _userProfileSagmentOutlet
具有三个部分。这是示例代码-
CGFloat fontSize = 15;
[_userProfileSagmentOutlet setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Roboto-medium" size:fontSize],
NSForegroundColorAttributeName:[UIColor whiteColor]}
forState:UIControlStateSelected];
[_userProfileSagmentOutlet setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Roboto-medium" size:fontSize],
NSForegroundColorAttributeName:[UIColor whiteColor]}
forState:UIControlStateNormal];
这是之前的代码,截断标题的尾部,如下图-
这里是主要的逻辑,将每个标题放入具有相同字体大小的段中-
CGFloat fontSize = 15;
NSAttributedString* firstTitle = [[NSAttributedString alloc] initWithString:@"Membership History" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Roboto-medium" size:fontSize]}];
NSAttributedString* secondTitle = [[NSAttributedString alloc] initWithString:@"Event History" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Roboto-medium" size:fontSize]}];
NSAttributedString* thirdTitle = [[NSAttributedString alloc] initWithString:@"Booked Classes" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Roboto-medium" size:fontSize]}];
float maxW=MAX(MAX(firstTitle.size.width, secondTitle.size.width), thirdTitle.size.width);
while (maxW > _userProfileSagmentOutlet.subviews[0].frame.size.width) {
fontSize--;
firstTitle = [[NSAttributedString alloc] initWithString:@"Membership History" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Roboto-medium" size:fontSize]}];
secondTitle = [[NSAttributedString alloc] initWithString:@"Event History" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Roboto-medium" size:fontSize]}];
thirdTitle = [[NSAttributedString alloc] initWithString:@"Booked Classes" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Roboto-medium" size:fontSize]}];
maxW=MAX(MAX(firstTitle.size.width, secondTitle.size.width), thirdTitle.size.width);
}
[_userProfileSagmentOutlet setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Roboto-medium" size:fontSize],
NSForegroundColorAttributeName:[UIColor whiteColor]}
forState:UIControlStateSelected];
[_userProfileSagmentOutlet setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Roboto-medium" size:fontSize],
NSForegroundColorAttributeName:[UIColor whiteColor]}
forState:UIControlStateNormal];
使用此代码后图像看起来像这样(相同的字体大小和文本适合分段并且工作正常)-
如果有人需要,这里是 Swift 分机-
var fontSize:CGFloat = 15.0;
var firstTitle = NSMutableAttributedString.init(string: "Membership History", attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)])
var secondTitle = NSMutableAttributedString.init(string: "Events History" ,attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)]);
var thirdTitle = NSMutableAttributedString.init(string: "Booked Classes" ,attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)]);
var maxW:CGFloat = max(max(firstTitle.size().width, secondTitle.size().width), thirdTitle.size().width);
while (maxW > userProfileSagmentOutlet.subviews[0].frame.size.width) {
fontSize--;
firstTitle = NSMutableAttributedString.init(string: "Membership History", attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)])
secondTitle = NSMutableAttributedString.init(string: "Events History" ,attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)]);
thirdTitle = NSMutableAttributedString.init(string: "Booked Classes" ,attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)]);
maxW = max(max(firstTitle.size().width, secondTitle.size().width), thirdTitle.size().width);
}
userProfileSagmentOutlet.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(fontSize),NSForegroundColorAttributeName:UIColor.whiteColor()], forState:UIControlState.Normal)
userProfileSagmentOutlet.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(fontSize),NSForegroundColorAttributeName:UIColor.whiteColor()], forState:UIControlState.Selected)
我发现问题了,原来是我的错!!!我将 numberOfLines,adjustsFontSizeToFitWidth,minimumScaleFactor
和 TitleTextAttributes
设置在一起。如果我们设置 titleTextAttribute
则 minimumScaleFactor
无法工作。
更新:(@HawkEye1194 在另一个答案的评论中提出)
我得到了以下解决方案,
//this will allow multiple lines in label contained by every segment in segmentedcontroller
[[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] setNumberOfLines:0];
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:option];
segment.frame = CGRectMake(20, 50, self.view.frame.size.width - 40, 50);
segment.tintColor = [UIColor grayColor];
segment.selectedSegmentIndex = 0;
segment.backgroundColor = [UIColor whiteColor];
segment.tag = segmentedControllerBaseTag;
[segment addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
[segment setTitleTextAttributes:@{NSFontAttributeName :[UIFont fontWithName:@"HelveticaNeue" size:17.0], NSForegroundColorAttributeName : [UIColor darkGrayColor] } forState:UIControlStateNormal];
[segment setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue" size:17.0],NSForegroundColorAttributeName : [UIColor whiteColor]} forState:UIControlStateSelected];
如果你没有像上面那样设置标题文本属性,那么你可以使用下面的代码
// ********** if titletextattributes are not set then below method works ***********
for(uint i=0;i<[segment subviews].count;i++)
{
for(UIView *view in [[[segment subviews] objectAtIndex:i] subviews])
{
if([view isKindOfClass:[UILabel class]])
{
[(UILabel*)view setNumberOfLines:0];
[(UILabel*)view setAdjustsFontSizeToFitWidth:YES];
[(UILabel*)view setMinimumScaleFactor:0.7];
}
}
}
您可以通过以下代码根据内容调整单个片段的大小,
//*************** adjust single segment size as per content
segment.apportionsSegmentWidthsByContent = YES;
SWIFT 4
标签中允许多行
if #available(iOS 9.0, *) {
UILabel.appearance(whenContainedInInstancesOf: [UISegmentedControl.self]).numberOfLines = 0
}
我运行在view controller中这个,在viewDidAppear之后调用它
func autoshrinkSegmentFontSize() {
for subview in segments.subviews {
for label in subview.subviews {
if let myLabel = subview as? UILabel {
myLabel.adjustsFontSizeToFitWidth = true
myLabel.minimumScaleFactor = 0.5
}
}
}
}
现代 Swift 中最简单的解决方案是
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
_ = Your_UISegmentControl.subviews.compactMap { [=10=].subviews.compactMap {
([=10=] as? UILabel)?.adjustsFontSizeToFitWidth = true
([=10=] as? UILabel)?.minimumScaleFactor = 0.5
}}
}
有没有什么方法可以减小适合 UISegmentedControl
的单个段的字体大小?
尝试过很多类似的东西,
[[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] adjustsFontSizeToFitWidth];
[[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] setMinimumScaleFactor:0.5];
和
NSArray *arr = segment.subviews; // segment is UISegmentedControl object
for (int i = 0; i < arr.count; i++) {
UIView *aSegment = [arr objectAtIndex:i];
for (UILabel *label in aSegment.subviews) {
if ([label isKindOfClass:[UILabel class]]) {
UILabel *myLabel = (UILabel *)label;
[myLabel setNumberOfLines:0];
label.numberOfLines = 0;
label.adjustsFontSizeToFitWidth = YES;
label.minimumScaleFactor = 0.5;
}
}
}
可以设置段标签的行数,如
[[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] setNumberOfLines:0];
可以根据内容设置单段大小,
segment.apportionsSegmentWidthsByContent = YES;
但在这种情况下每个段的大小都不同。
我想保持每个段的大小相同,并希望减小适合 UISegmentLabel (label)
或 UISegmentedControl
的字体大小,例如 minimumscalefactor
或 minimumfontsize
或adjustsFontSizeToFitWidth
。当包含在 UISegmentedControl
.
如果有人能帮助实现这一点,我们将不胜感激!!
提前致谢!!
试试这个,我希望这会对你有所帮助,你会明白这是如何工作的-
我有一个 UISegmentedControl
即 _userProfileSagmentOutlet
具有三个部分。这是示例代码-
CGFloat fontSize = 15;
[_userProfileSagmentOutlet setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Roboto-medium" size:fontSize],
NSForegroundColorAttributeName:[UIColor whiteColor]}
forState:UIControlStateSelected];
[_userProfileSagmentOutlet setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Roboto-medium" size:fontSize],
NSForegroundColorAttributeName:[UIColor whiteColor]}
forState:UIControlStateNormal];
这是之前的代码,截断标题的尾部,如下图-
这里是主要的逻辑,将每个标题放入具有相同字体大小的段中-
CGFloat fontSize = 15;
NSAttributedString* firstTitle = [[NSAttributedString alloc] initWithString:@"Membership History" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Roboto-medium" size:fontSize]}];
NSAttributedString* secondTitle = [[NSAttributedString alloc] initWithString:@"Event History" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Roboto-medium" size:fontSize]}];
NSAttributedString* thirdTitle = [[NSAttributedString alloc] initWithString:@"Booked Classes" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Roboto-medium" size:fontSize]}];
float maxW=MAX(MAX(firstTitle.size.width, secondTitle.size.width), thirdTitle.size.width);
while (maxW > _userProfileSagmentOutlet.subviews[0].frame.size.width) {
fontSize--;
firstTitle = [[NSAttributedString alloc] initWithString:@"Membership History" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Roboto-medium" size:fontSize]}];
secondTitle = [[NSAttributedString alloc] initWithString:@"Event History" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Roboto-medium" size:fontSize]}];
thirdTitle = [[NSAttributedString alloc] initWithString:@"Booked Classes" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Roboto-medium" size:fontSize]}];
maxW=MAX(MAX(firstTitle.size.width, secondTitle.size.width), thirdTitle.size.width);
}
[_userProfileSagmentOutlet setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Roboto-medium" size:fontSize],
NSForegroundColorAttributeName:[UIColor whiteColor]}
forState:UIControlStateSelected];
[_userProfileSagmentOutlet setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Roboto-medium" size:fontSize],
NSForegroundColorAttributeName:[UIColor whiteColor]}
forState:UIControlStateNormal];
使用此代码后图像看起来像这样(相同的字体大小和文本适合分段并且工作正常)-
如果有人需要,这里是 Swift 分机-
var fontSize:CGFloat = 15.0;
var firstTitle = NSMutableAttributedString.init(string: "Membership History", attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)])
var secondTitle = NSMutableAttributedString.init(string: "Events History" ,attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)]);
var thirdTitle = NSMutableAttributedString.init(string: "Booked Classes" ,attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)]);
var maxW:CGFloat = max(max(firstTitle.size().width, secondTitle.size().width), thirdTitle.size().width);
while (maxW > userProfileSagmentOutlet.subviews[0].frame.size.width) {
fontSize--;
firstTitle = NSMutableAttributedString.init(string: "Membership History", attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)])
secondTitle = NSMutableAttributedString.init(string: "Events History" ,attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)]);
thirdTitle = NSMutableAttributedString.init(string: "Booked Classes" ,attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)]);
maxW = max(max(firstTitle.size().width, secondTitle.size().width), thirdTitle.size().width);
}
userProfileSagmentOutlet.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(fontSize),NSForegroundColorAttributeName:UIColor.whiteColor()], forState:UIControlState.Normal)
userProfileSagmentOutlet.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(fontSize),NSForegroundColorAttributeName:UIColor.whiteColor()], forState:UIControlState.Selected)
我发现问题了,原来是我的错!!!我将 numberOfLines,adjustsFontSizeToFitWidth,minimumScaleFactor
和 TitleTextAttributes
设置在一起。如果我们设置 titleTextAttribute
则 minimumScaleFactor
无法工作。
更新:(@HawkEye1194 在另一个答案的评论中提出)
我得到了以下解决方案,
//this will allow multiple lines in label contained by every segment in segmentedcontroller
[[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] setNumberOfLines:0];
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:option];
segment.frame = CGRectMake(20, 50, self.view.frame.size.width - 40, 50);
segment.tintColor = [UIColor grayColor];
segment.selectedSegmentIndex = 0;
segment.backgroundColor = [UIColor whiteColor];
segment.tag = segmentedControllerBaseTag;
[segment addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
[segment setTitleTextAttributes:@{NSFontAttributeName :[UIFont fontWithName:@"HelveticaNeue" size:17.0], NSForegroundColorAttributeName : [UIColor darkGrayColor] } forState:UIControlStateNormal];
[segment setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue" size:17.0],NSForegroundColorAttributeName : [UIColor whiteColor]} forState:UIControlStateSelected];
如果你没有像上面那样设置标题文本属性,那么你可以使用下面的代码
// ********** if titletextattributes are not set then below method works ***********
for(uint i=0;i<[segment subviews].count;i++)
{
for(UIView *view in [[[segment subviews] objectAtIndex:i] subviews])
{
if([view isKindOfClass:[UILabel class]])
{
[(UILabel*)view setNumberOfLines:0];
[(UILabel*)view setAdjustsFontSizeToFitWidth:YES];
[(UILabel*)view setMinimumScaleFactor:0.7];
}
}
}
您可以通过以下代码根据内容调整单个片段的大小,
//*************** adjust single segment size as per content
segment.apportionsSegmentWidthsByContent = YES;
SWIFT 4
标签中允许多行
if #available(iOS 9.0, *) {
UILabel.appearance(whenContainedInInstancesOf: [UISegmentedControl.self]).numberOfLines = 0
}
我运行在view controller中这个,在viewDidAppear之后调用它
func autoshrinkSegmentFontSize() {
for subview in segments.subviews {
for label in subview.subviews {
if let myLabel = subview as? UILabel {
myLabel.adjustsFontSizeToFitWidth = true
myLabel.minimumScaleFactor = 0.5
}
}
}
}
现代 Swift 中最简单的解决方案是
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
_ = Your_UISegmentControl.subviews.compactMap { [=10=].subviews.compactMap {
([=10=] as? UILabel)?.adjustsFontSizeToFitWidth = true
([=10=] as? UILabel)?.minimumScaleFactor = 0.5
}}
}