IOS开发中常用的代码块收集整理

上传人:m**** 文档编号:51679087 上传时间:2018-08-15 格式:DOCX 页数:9 大小:36.53KB
返回 下载 相关 举报
IOS开发中常用的代码块收集整理_第1页
第1页 / 共9页
IOS开发中常用的代码块收集整理_第2页
第2页 / 共9页
IOS开发中常用的代码块收集整理_第3页
第3页 / 共9页
IOS开发中常用的代码块收集整理_第4页
第4页 / 共9页
IOS开发中常用的代码块收集整理_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《IOS开发中常用的代码块收集整理》由会员分享,可在线阅读,更多相关《IOS开发中常用的代码块收集整理(9页珍藏版)》请在金锄头文库上搜索。

1、IOSIOS 开发中常用的代码块收集整理开发中常用的代码块收集整理收集常用的代码块是加快开发的有效途径,收集的操作步骤这里不在赘述,仅仅贡献上一些常用的功能性代码1、与高度和宽度有关的? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34/*获取导航栏高度*/public func getNavigationBarHeight() - CGFloat let navRect:CGRect! = self.navigationController?.navigat

2、ionBar.framereturn navRect.size.height/*获取标签栏高度*/public func getTabBarHeight() - CGFloat let tabBarRect:CGRect! = self.tabBarController?.tabBar.framereturn tabBarRect.size.height/*获取状态栏高度*/public func getStatusBarHeight() - CGFloat let rectStatus:CGRect = UIApplication.shared.statusBarFramereturn re

3、ctStatus.size.height/*根据提供的字体大小,宽度获取字文字的高度*/func getTextHeigh(textStr:String,font:UIFont,width:CGFloat) - CGFloat let normalText: NSString = textStr as NSStringlet size = CGSize(width: width, height: 10000)let dic:NSDictionary = NSDictionary(object: font, forKey: NSAttributedStringKey.font as NSCopy

4、ing)let stringSize = normalText.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: dic as? NSAttributedStringKey : Any, context:nil).sizereturn stringSize.height/*提供文字大小,控件高度,获取到文字的宽度*/func getTexWidth(textStr:String,font:UIFont,height:CGFloat) - CGFloat let normalText: NSString

5、= textStr as NSString35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51let size = CGSize(width: 10000, height: height)let dic:NSDictionary = NSDictionary(object: font, forKey: NSAttributedStringKey.font as NSCopying)let stringSize = normalText.boundingRect(with: size, options: .usesLineFragmentOrigi

6、n, attributes: dic as? NSAttributedStringKey : Any, context:nil).sizereturn stringSize.width + 40UIScreen.main.bounds.size.width / 屏幕宽度UIScreen.main.bounds.size.height / 屏幕高度/*UIFont 的适配(创建 UIFont 的扩展)*/+ (void)load Method newMethod = class_getClassMethod(self class, selector(adjustFont:);Method met

7、hod = class_getClassMethod(self class, selector(systemFontOfSize:);method_exchangeImplementations(newMethod, method); + (UIFont *)adjustFont:(CGFloat)fontSize UIFont *newFont = nil;/BaseWidth 为设计图的尺寸newFont = UIFont adjustFont:fontSize * UIScreen mainScreen.bounds.size.width / BaseWidth;return newFo

8、nt; 2、处理类相关? 1 2 3 4 5 6 7 8 9 10 11 12 13/*十六进制颜色*/func RGBColorFromHex(rgbValue: Int) - (UIColor) return UIColor(red: (CGFloat)(rgbValue const char *cStr = str.UTF8String;unsigned char resultCC_MD5_DIGEST_LENGTH;CC_MD5(cStr, (CC_LONG)strlen(cStr), result);NSMutableString *md5Str = NSMutableString

9、string;for (int i = 0; i (_ filter: (Element) - E) - Element var result = Element()for value in self let key = filter(value) if !result.map(filter($0).contains(key) result.append(value) return result /*图片二维码识别(使用 ZXing)*/+ (void)recognizeImage:(UIImage*)image block:(void()(ZXBarcodeFormat barcodeFor

10、mat,NSString *str)block 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145ZXCGImageLuminanceSource *source = ZXCGImageLuminanceSource alloc initWithCGImage:image.CGImage;ZXHyb

11、ridBinarizer *binarizer = ZXHybridBinarizer alloc initWithSource: source;ZXBinaryBitmap *bitmap = ZXBinaryBitmap alloc initWithBinarizer:binarizer;NSError *error;id reader;if (NSClassFromString(“ZXMultiFormatReader“) reader = NSClassFromString(“ZXMultiFormatReader“) performSelector:selector(reader);

12、ZXDecodeHints *_hints = ZXDecodeHints hints;ZXResult *result = reader decode:bitmap hints:_hints error:if (result = nil) block(kBarcodeFormatQRCode,nil);return;block(result.barcodeFormat,result.text); /*swift 执行一次的 Dispatch_once 函数*/public extension DispatchQueue private static var _onceTracker = St

13、ring()public class func once(key: String, block:()-Void) objc_sync_enter(self)defer objc_sync_exit(self) if _onceTracker.contains(key) return_onceTracker.append(key)block() /*扩大按钮响应区域(创建 UIButton 扩展)*/捕鱼游戏 http:/static char topNameKey; static char rightNameKey; static char bottomNameKey; static char

14、 leftNameKey; - (void)setEnlargeEdgeWithTop:(CGFloat) top right:(CGFloat) right bottom:(CGFloat) bottom left:(CGFloat) left 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185objc_setAssociate

15、dObject(self, objc_setAssociatedObject(self, objc_setAssociatedObject(self, objc_setAssociatedObject(self, - (void)setEnlargeEdge:(CGFloat) size objc_setAssociatedObject(self, objc_setAssociatedObject(self, objc_setAssociatedObject(self, objc_setAssociatedObject(self, - (CGRect) enlargedRect NSNumbe

16、r* topEdge = objc_getAssociatedObject(self, NSNumber* rightEdge = objc_getAssociatedObject(self, NSNumber* bottomEdge = objc_getAssociatedObject(self, NSNumber* leftEdge = objc_getAssociatedObject(self, if (topEdge elsereturn self.bounds; - (UIView*) hitTest:(CGPoint) point withEvent:(UIEvent*) event CGRect rect = self enlargedRect;if (CGRectEq

展开阅读全文
相关资源
相关搜索

当前位置:首页 > IT计算机/网络 > 手机/mobile开发

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号