iOS Tips
在label中插入圖片
let label = UILabel()
let attribute = NSMutableAttributedString(string: "Title")
let imgAttch = NSTextAttachment()
imgAttch.image = image
//設置圖片大小
imgAttch.bounds = CGRect.init(x: 0, y: 0, width: 18, height: 15)
let imageAttribute = NSAttributedString(attachment: imgAttch)
//圖片插入位置
attribute.insert(imageAttribute, at: 0)
label.attributedText = attribute
複製代碼
CollectionCell陰影+圓角的優雅處理方式
只針對類似App Store的整體圓角陰影的效果。
//設置父視圖陰影效果
//CollectionView.swift
self.layer.shadowOffset = offset
self.layer.shadowColor = color.cgColor
self.layer.shadowRadius = radius
self.layer.shadowOpacity = opacity
contentView.layer.cornerRadius = radius
//如果切割圓角帶imageView才需要添加下面
contentView.layer.masksToBounds = true
複製代碼
處理tableView點擊時label背景色消失問題
//第一種方式,UITableViewCell.swift
override func setHighlighted(_ highlighted: Bool, animated: Bool) {
let color = self.indexLabel.backgroundColor
super.setHighlighted(highlighted, animated: animated)
self.indexLabel.backgroundColor = color
}
//第二種方式
cell.textLabel.backgroundColor = UIColor.clear
cell.textLabel.layer.backgroundColor = UIColor.red
複製代碼
調整UILabel文字的內邊距
自定義UILabel
,然後重寫drawText:
方法
override func drawText(in rect: CGRect) {
let insets = UIEdgeInsets.init(top: 20, left: 20, bottom: 20, right: 20)
super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))
}
複製代碼
swift 浮點數取整
ceil(x)返回不小於x的最小整數值。 floor(x)返回不大於x的最大整數值。 round(x)返回x的四捨五入整數值。
let number1 = 12.456
// ceil(number1) = 13.0, floor(number1) = 12.0, round(number1) = 12.0
let number2 = 12.756
// ceil(number2) = 13.0, floor(number2) = 12.0, round(number2) = 13.0
複製代碼
跳過非store下載的應用檢查
xattr -d com.apple.quarantine app所在路徑
加載大圖導致內存暴漲
large_leaves_70mp.jpg
圖片是7033x10110(佔用磁盤大小8.3MB),分辨率 = 7033 x 10110 x 4(ARGB),對應位圖佔用大小 = 分辨率 x 1024 x 1024 ( = 271MB),解壓會把圖片轉成位圖,也就意味着會佔用271MB內存,所以解壓過程內存會瞬間消耗很大,等轉成NSData後位圖的內存就會被回收掉,內存就降下來,這時候NSData佔用的大小即是圖片的實際大小,該過程中由於會轉成位圖,而位圖的大小是比圖片的實際的大小大很多的,內存暴增的點就在位圖。位圖的內存大小計算是根據圖片的分辨率而來(分辨率(width x heigth) x 1024 x 1024 x 4 (ARGB)),所以一般來說圖片分辨率越高轉成的位圖佔用的內存空間越大。
新版iphone尺寸
設備 | 邏輯分辨率 | 比例因子 | 對角線 | 分辨率 |
---|---|---|---|---|
iPhone XS Max | 414×896 | @3x | 6.5inch | 1242px × 2688px |
iPhone XS | 375×812 | @3x | 5.8inch | 1125px × 2436px |
iPhone XR | 414×896 | @2x | 6.1inch | 828px × 1792px |
iPhone X | 375×812 | @3x | 5.8inch | 1125px × 2436px |
iPhone 8 Plus | 414×736 | @3x | 5.5inch | 1242px × 2208px |
iPhone 8 | 375×667 | @3x | 4.7inch | 750px × 1334px |
iPhone SE | 320×568 | @2x | 4inch | 640px × 1136px |
iPhone 4 | 320×480 | @2x | 3.5inch | 640px × 960px |
帶透明度的漸變
使用CAGradientLayer進行漸變的時候,如果我們需要由一個顏色漸變至透明,當我們將透明色寫成UIColor.clear
或者其他通過RGBA
設置的顏色,改變透明度爲0時,會發現實際效果跟預期有出入,會帶點黑色:
這是因爲clearColor會有一個透明度爲0的黑色通道。所以應該這樣設置透明色:
UIColor(white: 1, alpha: 0).cgColor
複製代碼
指定tableView,collectionView的header高度0
如果我們想隱藏headerView
可能會直接在其高度的代理方法,做如下設置:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 0
}
複製代碼
當實際上這並不會生效,會返回一個默認高度。也就是說這個高度只有設置成一個非0的正數纔是有效的。 有些人會寫成0.01,表面上看是解決問題了,但0.01的偏移會造成像素不對齊(Color Misaligned Images),加重CPU計算負荷。完美的解決方案是:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return CGFloat.leastNonzeroMagnitude
}
複製代碼
CGFloat.leastNonzeroMagnitude
表示CGFloat支持的最小正數值,不會引起像素偏移。
保持屏幕常亮
//保持屏幕常亮
UIApplication.shared.isIdleTimerDisabled = true
//關閉屏幕常亮
UIApplication.shared.isIdleTimerDisabled = false
複製代碼
注意:不要濫用屏幕常亮屬性(蘋果會因爲這打回你的app),如果只在某些特殊場合需要屏幕常亮,應該在之後將該值恢復成默認值false
。
Github
MMKV
MMKV 是基於 mmap 內存映射的 key-value 組件,底層序列化/反序列化使用 protobuf 實現,性能高,穩定性強。從 2015 年中至今,在 iOS 微信上使用已有近 3 年,其性能和穩定性經過了時間的驗證。近期也已移植到 Android 平臺,一併開源。
替代UserDefaults
的絕佳方案。
Lottie
Lottie是一個面向Android和iOS的移動庫,它能夠解析由AE在bodymovin導出爲json的效果動畫,並在移動端渲染矢量動畫! 一些複雜的UI可以完全交給設計師了,哈哈哈。
RSSHud
RSSHub 是一個輕量、易於擴展的 RSS 生成器,可以給任何奇奇怪怪的內容生成 RSS 訂閱源。 結合Reeder不要太爽哦。😆