#import <Foundation/Foundation.h>
//! Project version number for SwiftyLoad.
FOUNDATION_EXPORT double SwiftyLoadVersionNumber;
//! Project version string for SwiftyLoad.
FOUNDATION_EXPORT const unsigned char SwiftyLoadVersionString[];
@protocol NSSwiftyLoadProtocol <NSObject>
@optional
+ (void)swiftyLoad;
+ (void)swiftyInitialize;
@end
#define SWIFTY_LOAD_INITIALIZE(className) \
@interface className(swizzle_swifty_hook)\
@end\
\
@implementation className(swizzle_swifty_hook)\
+ (void)load {if ([[self class] respondsToSelector:@selector(swiftyLoad)]) {[[self class] swiftyLoad];}}\
+ (void)initialize {if ([[self class] respondsToSelector:@selector(swiftyInitialize)]) {[[self class] swiftyInitialize];}}\
@end
extension UILabel : NSSwiftyLoadProtocol {
public static func swiftyInitialize() {
print("\(self)--->swiftyInitialize")
}
public static func swiftyLoad() {
print("\(self)--->swiftyLoad")
self.rtl_MethodSwizzling(fromMethod: #selector(setter: UILabel.textAlignment), toMethod: #selector(rtl_setTextAlignment(textAlignment:)))
}
@objc class func rtl_MethodSwizzling(fromMethod: Selector, toMethod: Selector) {
guard let method1 = class_getInstanceMethod(self, fromMethod) else { return }
guard let method2 = class_getInstanceMethod(self, toMethod) else { return }
method_exchangeImplementations(method1, method2)
}
@objc func rtl_setTextAlignment(textAlignment: NSTextAlignment) {
var alignment = textAlignment
if UILabel.isRTL() {
if textAlignment == .natural || textAlignment == .left {
alignment = .right
}else if textAlignment == .right {
alignment = .left
}
}
self.rtl_setTextAlignment(textAlignment: alignment)
}
}
swift替换系统方法
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 对于个人来说,开发中用到runtime的情况很少,而且个人也不建议过多使用runtime甚至滥用runtime,如...
- 很多iOS开发者都会遇到这么一个问题,当你调用系统的一个方法你发现这个方法的代码实现不满足你的需求(比如导航栏的返...
- 前两天,由于项目中后台数据的调整,导致我在加载WKWebView的时候,不能利用cookie事先保存自己的用户信息...
- 最近两天公司有点小悠闲,于是就自己瞎转悠。最近沉迷于Python的学习路上,手指忽然间痒痒了,就玩玩OC中的run...
- 大多数情况下,系统类为我们提供的方法已经能够满足开发的需求,但有时候需求中的功能使用原生方法很难实现,或者系统类提...