Fra*_*ank 2 format label uisegmentedcontrol ios
在我的应用程序中,我使用的是UISegmentedcontrol.因为三个段中的每个段中的标签跨越两行,所以我需要自定义UISegmentedcontrol.我想做两件事:(1)增加UISegmentedcontrol的高度,以及(2)使文本标签跨越两行
我在这里找到了#1的解决方案:iOS:更改UISegmentedcontrol的高度,在这里更改UISegmentedControl的高度,在这里http://iphonedevsdk.com/forum/iphone-sdk-development/74275-change-height-of-segmented- control.html
我无法找到如何使UISegmentedcontrol内的标签跨越两行.任何帮助将不胜感激!
干杯,弗兰克
试试这个......创建一个类别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];
| 归档时间: | 
 | 
| 查看次数: | 1566 次 | 
| 最近记录: |