Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.0.6 更新日志 #7

Open
xaoxuu opened this issue Apr 28, 2017 · 0 comments
Open

0.0.6 更新日志 #7

xaoxuu opened this issue Apr 28, 2017 · 0 comments

Comments

@xaoxuu
Copy link
Owner

xaoxuu commented Apr 28, 2017

localize all subviews!

之前我们实现了将某个控件的文本转换成支持多语言的文本NSLocalizedString详见 #3

而在0.0.6以后的版本,你可以用一行代码将本页面所有控件所有文本进行转换:

AXLocalizeAllSubviewsInView(self.view); // available(0.0.6)

这个方法建议在项目成型之前使用,避免了频繁更改造成代码混乱,也能够提高开发效率。

缺点是略微影响性能,可以在项目成型�到无需大改的阶段之后删掉此代码,手动替换。

// @xaoxuu: 以下这些方法提供更加灵活的实现,可按需使用。
AXLocalizeLabel(self.label);
AXLocalizeTextField(self.textField);
AXLocalizeTextView(self.textView);
AXLocalizeAllLabelsInView(self.view);
AXLocalizeAllTextFieldsInView(self.view);
AXLocalizeAllTextViewsInView(self.view);

其实只要不那么极致要求性能的话,只要别用在tableview的cell里,一直留在项目里也是没有多大影响的。

修复了弹出键盘时调整输入框frame的bug

计算了被遮挡的高度offset,将view上移offset的高度。而不是上移一个keyboard的高度。

// @xaoxuu: 获取view距离底部的高度
CGFloat superH = self.ax_superview.frame.size.height;
CGFloat superF = self.ax_superview.frame.origin.y;
CGFloat heightToBottom = kScreenH - superF - superH;
// @xaoxuu: 被遮挡的高度
CGFloat offset = keyBoardHeight - heightToBottom;
if (offset < 0) {
    offset = 0;
}

NSStringFrom...

现在也可以快速将BOOL值、float值转换成字符串了。

调整了将int值转换成字符串的函数命名

// available(0.0.6)
FOUNDATION_EXTERN NSString *NSStringFromInt(int x);
// deprecated(0.0.5)
FOUNDATION_EXTERN NSString *NSStringFromInt32(int x);

万能控制器跳转

由于初代版本的疏忽,此功能理应成为UINavigationController的方法,却错误地放在了UIViewController的分类里。0.0.6修复了此bug,同时,为简版省去了animated:参数,默认为YES

现在你可以通过如下方式调用:

简版:

// available(0.0.6)
[self.navigationController ax_pushViewControllerNamed:@"tmpVC"];
// deprecated(0.0.5)
[self ax_pushViewControllerNamed:@"tmpVC" animated:YES];

完整版:

// available(0.0.6)
[self.navigationController ax_pushViewControllerNamed:@"tmpVC" animated:YES completion:^(UIViewController * _Nonnull targetVC) {
    // push completed call back
    targetVC.title = @"tmp";
} fail:^(NSError * _Nonnull error) {
    // push failed call back
    AXLogError(error);
}];

// deprecated(0.0.5)
[self ax_pushViewControllerNamed:@"tmpVC" animated:YES completion:^(UIViewController * _Nonnull targetVC) {
    // push completed call back
    targetVC.title = @"tmp";
} fail:^(NSError * _Nonnull error) {
    // push failed call back
    AXLogError(error);
}];

hidesBottomBarWhenPushed

push到子控制器的时候隐藏底部tabbar。

此方法本来属于UIViewController,写在要push的viewController里。

如果一个导航控制器想要让所有push的子控制器都隐藏tabbar。

一般的做法是继承,在一个父类- (void)viewDidLoad中加上这么一行代码:

[self hidesBottomBarWhenPushed]; // available(0.0.6)

现在你有了另一种选择,“设置”这个导航控制器,让它push的子控制器都隐藏tabbar。

在导航控制器的- (void)viewDidLoad中加上这么一行代码:

[self ax_hidesBottomBarWhenPushed:YES]; // available(0.0.6)

作者注:

实现这个功能主要技术点是如何取消交换方法。

单纯地取消就是再交换一次,但是实际应用必须判断是否交换过,只有交换过才能再交换一次负负得正,否则会产生错乱。

为了判断是否交换过,我查阅了很多资料,也在很多技术群里询问,没有得到一个优雅的解决方案。只能采取比较笨的方法:就是在交换的时候做一个标记,通过标记值判断该方法指针是否指向原地址。

更多方式选出子视图

之前我们只有一个方法来让某个view的每一个具有某些特征的子视图去执行一段代码,即:

// 以class来选定某些子视图,例如所有的button、所有的label等
// 让所有的按钮的标题为蓝色
[self.view ax_eachSubview:[UIButton class] action:^(__kindof UIView * _Nonnull subview) {
    UIButton *btn = subview;
    [btn setTitleColor:[UIColor md_blue] forState:UIControlStateNormal]; 
}];

现在你不仅可以通过类来选定,还可以通过tag值tag值区间

// 打印出所有tag为100的子视图
[self.view ax_eachSubviewWithTag:100 action:^(__kindof UIView * _Nonnull subview) {
    AXLogOBJ(subview);
}];
// 打印出所有tag值在0~100的子视图
[self.view ax_eachSubviewWithTags:AXIntegerRangeMake(0, 100) action:^(__kindof UIView * _Nonnull subview) {
    AXLogOBJ(subview);
}];

对于常用控件,可以更方便地选取:

// 让所有的按钮的标题为蓝色
[self.view ax_eachButtonInvokeAction:^(__kindof UIButton * _Nonnull button) {
    [button setTitleColor:[UIColor md_blue] forState:UIControlStateNormal];
}];

支持的控件有:UIButton、UILabel、UITextField、UITextView、UIImageView

自己重写的基于这些控件的子控件也可以被选取出来。

UIViewFromNibNamed

在开发中我经常需要写这一段代码:

UIView *tmpView = [[NSBundle mainBundle] loadNibNamed:@"tmpView" owner:nil options:nil].firstObject;

于是就有了UIViewFromNibNamed函数:

UIView *tmpView = UIViewFromNibNamed(@"tmpView"); // available(0.0.6)

kScreenBounds

我们经常把这几个宏作为开发必备的宏:

// available(0.0.6)
#define kScreenBounds [UIScreen mainScreen].bounds
// available(0.0.1)
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
#define kScreenCenterX (0.5 * kScreenW)
#define kScreenCenterY (0.5 * kScreenH)

给UIImageView先填充个颜色

在开发初期,美工的UI素材还没有做好,我们的ImageView不给图的时候一般都是空白,不利于测试。

自己去找的话,,,我真的没有时间。

但是写个工具的时间还是有的:

// 填充随机色
[self.imageView ax_fillWithRandomColor]; // available(0.0.6)

当然,你想填充指定的颜色也是可以的:

// 填充颜色
[self.imageView ax_fillWithColor:[UIColor md_green]]; // available(0.0.6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant