lixiang1994 / LEEAlert

优雅的可自定义 Alert ActionSheet
MIT License
1.29k stars 203 forks source link

设置LEEAction的attributedTitle 显示异常 #156

Closed Engandend closed 1 year ago

Engandend commented 2 years ago

问题1: 设置LEEAction的attributedTitle 显示异常 问题2: 如何动态设置sheet的action个数

问题1:

在设置LEEAction的attributedTitle 中,设置了行间隔,显示异常

版本:1.5.1 xcode: 12.5.1 手机:iPhone 6 收集系统:12.4.7 代码:

[LEEAlert actionsheet].config
            .LeeAddAction(^(LEEAction * _Nonnull action) {
                action.attributedTitle = [self attrForDelete];
                action.height = 75;
                action.numberOfLines = 0;
                [action setClickBlock:^{

                }];
            }).LeeAddAction(^(LEEAction * _Nonnull action) {
                action.type = LEEActionTypeCancel;
                action.title = @"取消";
                action.titleColor = [UIColor systemGrayColor];
                action.font = [UIFont boldSystemFontOfSize:17];
            })
            .LeeShow();

- (NSMutableAttributedString *)attrForDelete {
    NSString *title = @"删除";
    NSString *desc = @"这是一个描述";

    NSString *string = [NSString stringWithFormat:@"%@\n%@",title,desc];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:string];
    [attr addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:17],
                          NSForegroundColorAttributeName:[UIColor systemRedColor],
    } range:[string rangeOfString:title]];
    [attr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13],
                          NSForegroundColorAttributeName:[UIColor systemGrayColor],
    } range:[string rangeOfString:desc]];

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:6];
    paragraphStyle.alignment = NSTextAlignmentCenter;
    [attr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, string.length)];
    return attr;
}

如果设置了行间距,显示异常,如果不设置行间距,显示正常 显示效果:实际显示效果

问题2

我期望对LEEAlert进行二次封装,比如:传入title,如果title有内容,才显示title否则不显示。再比如:我期望传入一个个数(比如3)来添加leeaction。为达到此效果,我才用将config用一个对象接受,然后使用这个对象进行点语法 在实际操作中发现并没什么用。代码如下

- (void)showSheet {
    LEEBaseConfigModel *config = [LEEAlert actionsheet].config;
    config.LeeAddAction(^(LEEAction * _Nonnull action) {
        action.title = @"删除";
        [action setClickBlock:^{

        }];
    }).LeeAddAction(^(LEEAction * _Nonnull action) {
        action.type = LEEActionTypeCancel;
        action.title = @"取消";
        action.titleColor = [UIColor systemGrayColor];
        action.font = [UIFont boldSystemFontOfSize:17];
    })
    .LeeShow();
}

能正常显示和不能正常显示的区别在前面2行中,如果config不用对象接受,直接 用点语法,能显示正常,如果用对象接受,在用这个对象进行点语法,sheet显示不出来

// 不能正常弹出
LEEBaseConfigModel *config = [LEEAlert actionsheet].config;
    config.LeeAddAction(^(LEEAction * _Nonnull action) {
.......

// 能正常弹出
[LEEAlert actionsheet].config
.LeeAddAction(^(LEEAction * _Nonnull action) {
....
szmichaelyb commented 2 years ago

第2个问题,我二次封装,是可以动态添加的。 从你代码的实现角度,我认为应该是可以成功的,唯一的问题,只能猜测 actionArr 数组是不是你 传nil 过去了

Engandend commented 2 years ago

第2个问题,我二次封装,是可以动态添加的。 从你代码的实现角度,我认为应该是可以成功的,唯一的问题,只能猜测 actionArr 数组是不是你 传nil 过去了

问题已重新编辑,发现依然不行,兄弟能否给出二次封装的参考代码?

lixiang1994 commented 2 years ago

第二个问题:

LEEBaseConfig *actionsheet = [LEEAlert actionsheet];

            actionsheet.config
            .LeeTitle(@"标题")
            .LeeContent(@"内容");

            for (int i = 0; i < 10; i++) {
                actionsheet.config.LeeAction(@"好的", ^{

                    // 点击事件Block
                });
            }

            actionsheet.config.LeeShow();

@Engandend

lixiang1994 commented 2 years ago

第一个问题: LEEAction本质是UIButton, 所以你可以设置一下 action.lineBreakMode = NSLineBreakByWordWrapping;