【问题标题】:Make the label in an iOS UISegmentedcontrol span 2 lines使 iOS UISegmentedcontrol 中的标签跨 2 行
【发布时间】:2014-12-02 16:55:21
【问题描述】:

在我的应用程序中,我使用的是 UISegmentedcontrol。因为三个段中的每一个中的标签都跨越两行,所以我需要自定义 UISegmented 控件。我想做两件事:(1)增加UISegmentedcontrol的高度,以及(2)使文本标签跨越两行

我在这里找到了#1 的解决方案:iOS: change the height of UISegmentedcontrol 和这里 Change Height of UISegmentedControl 和这里 http://iphonedevsdk.com/forum/iphone-sdk-development/74275-change-height-of-segmented-control.html

我不知道如何使 UISegmentedcontrol 内的标签跨越两行。任何帮助将不胜感激!

干杯, 弗兰克

【问题讨论】:

  • 可能将标签的行数设置为零。

标签: ios format label uisegmentedcontrol


【解决方案1】:

试试这个... 为UISegmentedControl 创建一个类别,假设UISegmentedControl+Multiline

UISegmentedControl+Multiline.h

#import <UIKit/UIKit.h>

@interface UISegmentedControl (Multiline)

- (void)insertSegmentWithMultilineTitle:(NSString *)title atIndex:(NSUInteger)segment animated:(BOOL)animated;

@end

UISegmentedControl+Multiline.m

#import "UISegmentedControl+Multiline.h"

@interface UIView (LayerShot)

- (UIImage *)imageFromLayer;

@end

@implementation UIView (LayerShot)

- (UIImage *)imageFromLayer {
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end

@implementation UISegmentedControl (Multiline)

- (void)insertSegmentWithMultilineTitle:(NSString *)title atIndex:(NSUInteger)segment animated:(BOOL)animated {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    [label setTextColor:[self tintColor]];
    [label setBackgroundColor:[UIColor clearColor]];
    [label setFont:[UIFont systemFontOfSize:13]];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setLineBreakMode:NSLineBreakByWordWrapping];
    [label setNumberOfLines:0];

    [label setText:title];
    [label sizeToFit];

    [self insertSegmentWithImage:[label imageFromLayer] atIndex:segment animated:animated];
}
@end

最后在你的类中导入UISegmentedControl+Multiline.h并使用如下

 UISegmentedControl * segmentControl = [[UISegmentedControl alloc]initWithFrame:CGRectMake(3, 66, 314, 30)];

    [segmentControl insertSegmentWithMultilineTitle:@"First\nLine" atIndex:0 animated:YES];
    [segmentControl insertSegmentWithMultilineTitle:@"Second\nLine" atIndex:1 animated:YES];
    [segmentControl insertSegmentWithMultilineTitle:@"Third\nLine" atIndex:2 animated:YES];
    [segmentControl setSelectedSegmentIndex:0];
    [segmentControl addTarget:self action:@selector(segmentControlClicked:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:segmentControl];

【讨论】:

    猜你喜欢
    • 2016-03-18
    • 2018-02-02
    • 2014-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    相关资源
    最近更新 更多