美文网首页iOS Developer
UIAlertController设置标题/内容最大换行数和文本

UIAlertController设置标题/内容最大换行数和文本

作者: Everdinner | 来源:发表于2018-07-06 10:11 被阅读53次

前言

项目中用到的弹框组件是继承自UIAlertController修改使用的,像一些简单的颜色值和文字大小的修改已经有比较成熟的方案[iOS]改变UIAlertController的标题、内容的字体和颜色,在此不做重复介绍,本文主要介绍的是如何实现设置标题和内容的最大换行书和文本对齐方式

正文

UIAlertController默认的标题和内容是可以无限换行的,就像这样


alert.png

整个弹框内容已经超出了屏幕显示范围,而系统本身是没有提供属性供我们设置文本的最大换行数和对齐方式的,为了实现这个效果,我们选择在alertCotroller的viewDidLoad方法内获取标题视图和内容视图,这样就可以对最大换行数和对齐方式进行设置。

我们采用循环遍历子视图的方式取到两个label视图:

@interface UIView (LYViewHierarchy)

- (void)logViewHierarchy;
- (NSArray *)labelsForTitle:(NSString *)title message:(NSString *)message;

@end

@implementation UIView (LYViewHierarchy)

static NSMutableArray *_labelsArray;
static NSString *_title;
static NSString *_message;
static NSUInteger _totalCount;

- (void)logViewHierarchy {
    for (UIView *subview in self.subviews) {
        if ([subview isKindOfClass:[UILabel class]]) {
            UILabel *label = (UILabel *)subview;
            //title label
            if ([label.text isEqualToString:_title]) {
                [_labelsArray addObject:label];
            } else if ([label.text isEqualToString:_message]) {
            //message label
                [_labelsArray addObject:label];
            }
        }
        if (_labelsArray.count >= 2) { break; }
        [subview logViewHierarchy];
    }
}

- (NSArray *)labelsForTitle:(NSString *)title message:(NSString *)message {
    _totalCount = 0;
    _labelsArray = [NSMutableArray array];
    _title = title;
    _message = message;
    _totalCount = 0;
    if (_title != nil) {
        _totalCount += 1;
    }
    if (_message != nil) {
        _totalCount += 1;
    }
    //遍历所有子视图,找到对应title和message的label
    [self logViewHierarchy];
    return _labelsArray;
}

@end

我们选择继承UIAlertController来实现设置最大换行数:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTextAttribute];
}

- (void)setupTextAttribute {
    //设置标题和内容最大显示行数和对齐方式
    NSArray *labelsArray = [self.view labelsForTitle:self.title message:self.message];
    UILabel *titleLabel = labelsArray.firstObject;
    UILabel *messageLabel = labelsArray.count >= 2 ? labelsArray[1] : nil;
    if ([titleLabel isKindOfClass:[UILabel class]]) {
        titleLabel.numberOfLines = self.maxTitleNumberOfLines;
        titleLabel.textAlignment = self.titleTextAlignment;
        self.ly_titleLabel = titleLabel;
    }
    if ([messageLabel isKindOfClass:[UILabel class]]) {
        messageLabel.numberOfLines = self.maxMessageNumberOfLines;
        messageLabel.textAlignment = self.messageTextAlignment;
        self.ly_messageLabel = messageLabel;
    }
}

我们在继承类添加属性,可以设置标题和内容最大换行数以及对齐方式

@interface LYAlertController : UIAlertController

/* 标题最大显示行数 */
@property (nonatomic, assign) NSInteger maxTitleNumberOfLines;
/* 内容最大显示行数 */
@property (nonatomic, assign) NSInteger maxMessageNumberOfLines;
/* 标题对齐方式 */
@property (nonatomic, assign) NSTextAlignment titleTextAlignment;
/* 内容对齐方式 */
@property (nonatomic, assign) NSTextAlignment messageTextAlignment;

@end

我们选择设置标题最大换行数两行,内容最大换行数三行:

LYAlertController *alertController = [LYAlertController ly_alertControllerWithTitle:self.ly_title message:self.message cancelTitle:@"取消" confirmTitle:@"确认" handler:^(UIAlertAction *action) {
        NSLog(@"%@",action.title);
    }];
    alertController.maxTitleNumberOfLines = 2;
    alertController.maxMessageNumberOfLines = 3;
    [self presentViewController:alertController animated:YES completion:nil];

显示效果如下:


alert.png

如果你项目中有这种产品需求,不妨做一下尝试

最后附上Demo链接:LYAlertControllerDemo

相关文章

网友评论

    本文标题:UIAlertController设置标题/内容最大换行数和文本

    本文链接:https://www.haomeiwen.com/subject/jcbluftx.html