swift 开发过程中的一些小总结
2017-12-11 14:50
141 查看
1. 计算字符串放进label中的高度
let
size = CGSize(width:WIDTH-20,
height:0)
let
dic = NSDictionary(object:UIFont.systemFont(ofSize:14),
forKey: NSFontAttributeNameasNSCopying)
let
strSize = videoBody.summary?.boundingRect(with:
size, options: .usesLineFragmentOrigin,
attributes: dic as?
[String
: Any],
context: nil).size
self.label.frame
= CGRect(x:0,y:0,width:WIDTH-20,height:strSize?.height!)
2.一些系统通知
打断时通知
NotificationCenter.default.addObserver(self,
selector: #selector(onAudioSessionEvent),
name:NSNotification.Name.AVAudioSessionInterruption
, object: nil)
2.进入后台的通知
NotificationCenter.default.addObserver(self,
selector: #selector(onAppDidEnterBackGround),
name: NSNotification.Name.UIApplicationDidEnterBackground,
object: nil)
3.将要进入前台的通知
NotificationCenter.default.addObserver(self,
selector: #selector(onAppWillEnterForeground),
name: NSNotification.Name.UIApplicationWillEnterForeground,
object: nil)
APP活跃状态的通知
NotificationCenter.default.addObserver(self,
selector: #selector(onAppDidBecomeActive),
name: NSNotification.Name.UIApplicationDidBecomeActive,
object: nil)
屏幕发生旋转的通知
NotificationCenter.default.addObserver(self,
selector: #selector(statusBarOrientationChanged),
name: NSNotification.Name.UIApplicationDidChangeStatusBarOrientation,
object: nil)
3.GCD多线程
DispatchQueue.global().async
{
//耗时操作
DispatchQueue.main.async
{
//通知ui刷新
}
}
4.求余运算
5.字符串[b]format拼接[/b]
url:String(format:"%@/activity/listActivitys?start=%ld&limit=%ld",arguments[BASEADDRESS,withStart,withLimit])
6.判断数组是否有某元素 contains where
判断self.titleBodyArr数组中是否有id == 11的body
let has =self.titleBodyArr.contains(where:
{ (bo:FLGPindaoBody) ->Boolin
bo.id == 11
})
if has ==false{
self.dataArr.append(body)
}
7.md5 加密
创建文件
加入代码
import
Foundation
extensionString
{
var
md5 : String{
let
str = self.cString(using:String.Encoding.utf8)
let
strLen = CC_LONG(self.lengthOfBytes(using:String.Encoding.utf8))
let
digestLen = Int(CC_MD5_DIGEST_LENGTH)
let
result = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity:
digestLen)
CC_MD5(str!,
strLen, result)
let
hash = NSMutableString()
for
i in0
..< digestLen {
hash.appendFormat("%02x",
result[i])
}
result.deinitialize()
returnString(format:
hash asString)
}
}
在桥接文件中引头文件
#import<CommonCrypto/CommonDigest.h>
调用方法: let
passStr = pwd.md5
8. 图片 大小适应 UIimage
1. UIimageview 按图片最短边 完整填充 不变形
bigImageView?.contentScaleFactor =UIScreen.main.scale
bigImageView?.contentMode =UIViewContentMode.scaleAspectFill
bigImageView?.autoresizingMask =UIViewAutoresizing.flexibleHeight
bigImageView?.clipsToBounds =true
2.图片完全显示 不变形
bigImageView?.contentScaleFactor = UIScreen.main.scale
bigImageView?.contentMode = UIViewContentMode.scaleAspectFit
bigImageView?.clipsToBounds = true
let
size = CGSize(width:WIDTH-20,
height:0)
let
dic = NSDictionary(object:UIFont.systemFont(ofSize:14),
forKey: NSFontAttributeNameasNSCopying)
let
strSize = videoBody.summary?.boundingRect(with:
size, options: .usesLineFragmentOrigin,
attributes: dic as?
[String
: Any],
context: nil).size
self.label.frame
= CGRect(x:0,y:0,width:WIDTH-20,height:strSize?.height!)
2.一些系统通知
打断时通知
NotificationCenter.default.addObserver(self,
selector: #selector(onAudioSessionEvent),
name:NSNotification.Name.AVAudioSessionInterruption
, object: nil)
2.进入后台的通知
NotificationCenter.default.addObserver(self,
selector: #selector(onAppDidEnterBackGround),
name: NSNotification.Name.UIApplicationDidEnterBackground,
object: nil)
3.将要进入前台的通知
NotificationCenter.default.addObserver(self,
selector: #selector(onAppWillEnterForeground),
name: NSNotification.Name.UIApplicationWillEnterForeground,
object: nil)
APP活跃状态的通知
NotificationCenter.default.addObserver(self,
selector: #selector(onAppDidBecomeActive),
name: NSNotification.Name.UIApplicationDidBecomeActive,
object: nil)
屏幕发生旋转的通知
NotificationCenter.default.addObserver(self,
selector: #selector(statusBarOrientationChanged),
name: NSNotification.Name.UIApplicationDidChangeStatusBarOrientation,
object: nil)
3.GCD多线程
DispatchQueue.global().async
{
//耗时操作
DispatchQueue.main.async
{
//通知ui刷新
}
}
4.求余运算
let
value1 = 5.5
let
value2 = 2.2
let
result = value1.truncatingRemainder(dividingBy: value2)
5.字符串[b]format拼接[/b]
url:String(format:"%@/activity/listActivitys?start=%ld&limit=%ld",arguments[BASEADDRESS,withStart,withLimit])
6.判断数组是否有某元素 contains where
判断self.titleBodyArr数组中是否有id == 11的body
let has =self.titleBodyArr.contains(where:
{ (bo:FLGPindaoBody) ->Boolin
bo.id == 11
})
if has ==false{
self.dataArr.append(body)
}
7.md5 加密
创建文件
加入代码
import
Foundation
extensionString
{
var
md5 : String{
let
str = self.cString(using:String.Encoding.utf8)
let
strLen = CC_LONG(self.lengthOfBytes(using:String.Encoding.utf8))
let
digestLen = Int(CC_MD5_DIGEST_LENGTH)
let
result = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity:
digestLen)
CC_MD5(str!,
strLen, result)
let
hash = NSMutableString()
for
i in0
..< digestLen {
hash.appendFormat("%02x",
result[i])
}
result.deinitialize()
returnString(format:
hash asString)
}
}
在桥接文件中引头文件
#import<CommonCrypto/CommonDigest.h>
调用方法: let
passStr = pwd.md5
8. 图片 大小适应 UIimage
1. UIimageview 按图片最短边 完整填充 不变形
bigImageView?.contentScaleFactor =UIScreen.main.scale
bigImageView?.contentMode =UIViewContentMode.scaleAspectFill
bigImageView?.autoresizingMask =UIViewAutoresizing.flexibleHeight
bigImageView?.clipsToBounds =true
2.图片完全显示 不变形
bigImageView?.contentScaleFactor = UIScreen.main.scale
bigImageView?.contentMode = UIViewContentMode.scaleAspectFit
bigImageView?.clipsToBounds = true
相关文章推荐
- iPhone 开发过程中的一些小技术的总结(转)
- dwz问题(本文主要是供个人学习成长之用,陈述的是本人在开发过程中遇到的几个小问题的总结,可能兼或插入一些网络资料)
- ATL 开发 COM 过程中的一些经验、问题总结
- iPhone 开发过程中的一些小技术的总结
- rails3.2 开发过程中的一些总结
- SAP R/3 ABAP 开发过程中一些总结
- iPhone 开发过程中的一些小技术的总结
- iPhone 开发过程中的一些小技术的总结
- iPhone 开发过程中的一些小技术的总结
- 【原创】对于一些Android开发过程中坑爹、细小,但又重要的错误的总结
- WebGame开发过程中的一些思考和总结
- iPhone 开发过程中的一些小技术的总结
- iPhone 开发过程中的一些小技术的总结
- iPhone 开发过程中的一些小技术的总结
- [iPhone] 开发过程中的一些小技术的总结
- iPhone 开发过程中的一些小技术的总结
- 前端开发过程中的一些小知识点总结
- iPhone 开发过程中的一些小技术的总结
- iPhone 开发过程中的一些小技术的总结
- iOS 开发过程中常出现的一些错误总结