xcode UISegmentedControl 文本有多行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5614284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UISegmentedControl text with multiple lines?
提问by Jason
How can I make the text in one of the buttons in my UISegmentedControl span multiple lines?
如何使 UISegmentedControl 中的一个按钮中的文本跨越多行?
回答by Saranya Sivanandham
Use UIAppearance to get things done. The below code snippet will work. Call this before creating your segment.
使用 UIAppearance 完成任务。下面的代码片段将起作用。在创建细分之前调用它。
Objective-C
目标-C
[[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] setNumberOfLines:0];
Swift
迅速
UILabel.appearanceWhenContainedInInstancesOfClasses([UISegmentedControl.self]).numberOfLines = 0
回答by Jenson
I did it this way:
我是这样做的:
- create a multiline UILabel
- fill the label with N lines of text
- convert the label into an UIImage
- set the image as a segments content
- 创建一个多行 UILabel
- 用 N 行文本填充标签
- 将标签转换为 UIImage
- 将图像设置为片段内容
This works smooth on iOS 4, 5, 6
这在 iOS 4、5、6 上运行流畅
and iOS 7 (just remove the text shadow)
和 iOS 7(只需删除文本阴影)
MultiLineSegmentedControl - header file
MultiLineSegmentedControl - 头文件
//
// MultiLineSegmentedControl.h
//
// Created by Jens Kreiensiek on 20.07.11.
// Copyright 2011 SoButz. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface MultiLineSegmentedControl : UISegmentedControl
- (void)setMultilineTitle:(NSString *)title forSegmentAtIndex:(NSUInteger)segment;
@end
MultiLineSegmentedControl - implementation file
MultiLineSegmentedControl - 实现文件
//
// MultiLineSegmentedControl.m
//
// Created by Jens Kreiensiek on 20.07.11.
// Copyright 2011 SoButz. All rights reserved.
//
#import "MultiLineSegmentedControl.h"
#import "UIView+LayerShot.h"
@interface MultiLineSegmentedControl()
@property (nonatomic, retain) UILabel *theLabel;
@end
@implementation MultiLineSegmentedControl
@synthesize theLabel;
- (void)dealloc
{
self.theLabel = nil;
[super dealloc];
}
- (UILabel *)theLabel
{
if (!self->theLabel) {
self->theLabel = [[UILabel alloc] initWithFrame:CGRectZero];
self->theLabel.textColor = [UIColor whiteColor];
self->theLabel.backgroundColor = [UIColor clearColor];
self->theLabel.font = [UIFont boldSystemFontOfSize:13];
self->theLabel.textAlignment = UITextAlignmentCenter;
self->theLabel.lineBreakMode = UILineBreakModeWordWrap;
self->theLabel.shadowColor = [UIColor darkGrayColor];
self->theLabel.numberOfLines = 0;
}
return self->theLabel;
}
- (void)setMultilineTitle:(NSString *)title forSegmentAtIndex:(NSUInteger)segment
{
self.theLabel.text = title;
[self.theLabel sizeToFit];
[self setImage:self.theLabel.imageFromLayer forSegmentAtIndex:segment];
}
@end
UIView+LayerShot - header file
UIView+LayerShot - 头文件
//
// UIView+LayerShot.h
//
// Created by Jens Kreiensiek on 29.06.12.
// Copyright (c) 2012 SoButz. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIView (LayerShot)
- (UIImage *)imageFromLayer;
@end
UIView+LayerShot - implementation file
UIView+LayerShot - 实现文件
//
// UIView+LayerShot.m
//
// Created by Jens Kreiensiek on 29.06.12.
// Copyright (c) 2012 SoButz. All rights reserved.
//
#import "UIView+LayerShot.h"
#import <QuartzCore/QuartzCore.h>
@implementation UIView (LayerShot)
- (UIImage *)imageFromLayer
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end
Use it just like a normal UISegmentedControl:
像普通的 UISegmentedControl 一样使用它:
...
MultiLineSegmentedControl *segment = [[MultiLineSegmentedControl alloc]
initWithItems:[NSArray arrayWithObjects:@"A", @"B", nil]];
segment.segmentedControlStyle = UISegmentedControlStyleBar;
segment.frame = CGRectMake(0, 0, 200, segment.frame.size.height * 1.5);
[segment setMultilineTitle:@"Title A\nSubtitle A" forSegmentAtIndex:0];
[segment setMultilineTitle:@"Title B\nSubtitle B" forSegmentAtIndex:1];
[self.view addSubview:segment];
[segment release];
...
回答by Maverick
回答by Snowman
The approach above is better, but for the sake of having an alternative, you can do something like:
上面的方法更好,但为了有一个替代方案,您可以执行以下操作:
for(UIView *subview in segmentedControl.subviews) {
if([NSStringFromClass(subview.class) isEqualToString:@"UISegment"]) {
for(UIView *segmentSubview in subview.subviews) {
if([NSStringFromClass(segmentSubview.class) isEqualToString:@"UISegmentLabel"]) {
UILabel *label = (id)segmentSubview;
label.numberOfLines = 2;
label.text = @"Hello\nWorld";
CGRect frame = label.frame;
frame.size = label.superview.frame.size;
label.frame = frame;
}
}
}
}
回答by Emilio Hoffmann
Years later...
多年后...
for segment in segmented.subviews{
for label in segment.subviews{
if let labels = label as? UILabel{
labels.numberOfLines = 2
}
}
}
回答by R. Mohan
For iOS 12, the below code will work like charm
对于iOS 12,下面的代码将像魅力一样工作
[[UILabel appearanceWhenContainedInInstancesOfClasses:@[[UISegmentedControl class]]] setNumberOfLines:0];