iOS NerdyUI使用心得

作者: 一粒咸瓜子 | 来源:发表于2021-08-08 19:04 被阅读0次

NerdyUI:一个快速布局 UI 库,适用于 iOS 8 及以上版本。(中文说明)

1、优点

1.1 提供swift中的高阶函数

如果不想引入,可以自己仿写实现,例如《OC实现map、filter、reduce等高阶函数》

1.2 EXP

2、遇到的问题

2.1 使用 OnClick、OnLink...时注意循环引用

解决方法:用其提供的WeakifySelf()

2.2 Label.color导致崩溃

XCode11.4 模拟器iPhone8 Demo闪退 Thread 1: EXC_BAD_ACCESS #16
解决办法:

  • 手动设置color,不使用其提供的宏设置
  • 关闭Main Thread Checker服务。
    Main Thread Checker开启之后,不知道为什么会导致UIKit里UILabel一个废弃的方法被引入进来。这个方法的名字也是color。出问题时就是color实际上执行了这个方法,然后用返回的UIColor类型的值去当作block执行继而导致了崩溃。

也可以使用分类的前向引用来交换color方法解决问题:

@interface UILabel (fixColorBug)

@end
@implementation UILabel(fixColorBug)

+ (void)load
{
    Method method = class_getInstanceMethod([self class], @selector(color));
    Method methodNew = class_getInstanceMethod([self class], @selector(fcb_color));
    method_exchangeImplementations(method, methodNew);
}

- (id)fcb_color{
    id color = [self fcb_color];
    if (color == nil || [color isKindOfClass:[UIColor class]]) {
        //deprecated method
        return ^(id color){
            self.textColor = Color(color);
            return self;
        };
    }else{
        return color;
    }
}
@end

2.3 button 的multiline方法失效


在 iOS13 下 self.titleLabel.lineBreakMode = NSLineBreakByWordWrapping; 会自动设置换行,而 iOS14不会了,所以注意手动设置 btn.titleLabel.multiline

2.4 foreach方法在release环境中数组为nil时会崩溃

解决方法:过滤空数组,使用foreach时要谨慎


3、Demo

3.1 价格字符串

/** 价格格式化 */
- (void)moneyFormatter {
    NSAttributedString *attr = [self moneyAttrStrWithSymbol:@"¥"
                                                      value:@"99.99"
                                                 symbolFont:Fnt(20)
                                                  valueFont:Fnt(40)
                                                decimalFont:Fnt(20)
                                             symbolColorStr:@"red"
                                              valueColorStr:@"red"];
    
    Label.str(attr).fitSize.addTo(self.view).makeCons(^{
        make.center.equal.superview.constants(0);
    });
    
    
}

- (NSAttributedString *)moneyAttrStrWithSymbol:(NSString *)symbol
                                         value:(NSString *)value
                                    symbolFont:(UIFont *)symbolFont
                                     valueFont:(UIFont *)valueFont
                                   decimalFont:(UIFont *)decimalFont
                                symbolColorStr:(NSString *)symbolColorStr
                                 valueColorStr:(NSString *)valueColorStr {
    
    return AttStr(AttStr(symbol).fnt(symbolFont).color(symbolColorStr),
                  AttStr([value.subTo(@".") length] ? value.subTo(@".") : value).fnt(valueFont).color(valueColorStr),
                  AttStr(value.subFrom(@".")).fnt(decimalFont).color(valueColorStr));
}

3.2 隐私政策

/** 隐私协议超链 */
- (void)private {
    
    UIButton *icon = Button.img(@"icon_unselected").selectedImg(@"icon_selected").onClick(^(UIButton *btn){
        btn.selected = !btn.isSelected;
    }).touchInsets(-10,-10,-10,-10);
    
    NSAttributedString *attr = AttStr(@"登录注册后代表您已经认真阅读并同意《用户协议》和《隐私政策》,如您不是消费者,或不同意服务政策或隐私协议,您应立即停止使用本应用与相关服务。").fnt(14).match(@"《(.*?)》").color(@"blue").linkForLabel;
    
    HorStack(icon.fixWH(20,20),
             @20,
             Label.str(attr).lines(0).lineGap(8).onLink(^(NSString *text) {
                    NSLog(@"%@", text);
                })
             ).embedIn(self.view, NERNull, 20, 50, 20);
}

3.3 NerStack使用

/** stackView示例 */
- (void)nerStackDemo {
    id title = AttStr(@"中签300股").color(@"black").fnt(20).matchNumber.color(@"red");
    UILabel *numberLab = Label.str(@"02263").fnt(13);

    VerStack(HorStack(ImageView.fixWH(30,20).img(@"red"), @(10), Label.fnt(15).str(@"京东集团")),
            @(10),
            numberLab,
            @(30),
            Label.str(title)
            ).centerAlignment.addTo(self.view).makeCons(^{
        make.width.equal.constants(200);
        make.center.equal.superview.constants(0);
    });
}

3.4 图文混排

/** 图文混排 */
- (void)图文混排 {
    
    [self createTypeButtonWithTitle:@"Wechat" imageName:@"icon_wechat" imageSize:CGSizeMake(50, 50) lineGap:8].addTo(self.view).makeCons(^{
        make.center.equal.superview.constants(0);
    });
    
}

- (UIButton *)createTypeButtonWithTitle:(NSString *)title imageName:(NSString *)imageName imageSize:(CGSize)size lineGap:(CGFloat)lineGap {
    
    NSAttributedString *attrStr = AttStr(Img(imageName).resize(size.width, size.height), AttStr(@"\n".a(title)).fnt(14).color(@"#b87f9b")).lineGap(lineGap).centerAlignment;
    
    UIButton *btn = Button.str(attrStr).multiline.highBgImg(@"0xf8f8f8");
    (void)btn.titleLabel.multiline;
    return btn;
}

相关文章

网友评论

    本文标题:iOS NerdyUI使用心得

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