最近又開始寫不少業(yè)務(wù)代碼了,有些小知識(shí)點(diǎn)小坑,用這個(gè)系列記錄一下。
iOS 開發(fā)小記-01
iOS 開發(fā)小記-02
1. UITextView 修改部分字體顏色,換行垂直居中
// 使用 \n 來換行
let attributeString = NSMutableAttributedString(string: "測(cè)試文字測(cè)試文字測(cè)試文字測(cè)試文字,高亮文字高亮文字高亮文字,\n換行后的文字換行后的文字")
attributeString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.black, range: NSMakeRange(0, attributeString.length))
// 使用 NSMakeRage 來修改部分字體顏色
attributeString.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(0, attributeString.length))
attributeString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.cyan, range: NSMakeRange(17, 12))
textView.attributedText = attributeString
// 注意這個(gè) textAligment 需要在 attributedText 賦值后調(diào)用,不然不生效
textView.textAlignment = .center
注意:UITextView 居中(textAlignment) 不生效問題

image.png
2. 應(yīng)用內(nèi)禁用 DarkMode
在 Info.plist 中添加 User Interface Style : Light,即可在應(yīng)用內(nèi)禁用暗黑模式。
3. 給 UIView 添加半邊圓角
let maskPath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: 240, height: 40),
byRoundingCorners: [.topRight, .bottomRight],
cornerRadii: CGSize(width: 20, height: 20))
let roundCornerShape = CAShapeLayer()
roundCornerShape.path = maskPath.cgPath
self.layer.mask = roundCornerShape
4. 給 URL 添加參數(shù)的方法
extension URL {
func appending(_ queryItem: String, value: String?) -> URL {
guard var urlComponents = URLComponents(string: absoluteString) else { return absoluteURL }
let queryItem = URLQueryItem(name: queryItem, value: value)
var queryItems: [URLQueryItem] = urlComponents.queryItems ?? []
queryItems.append(queryItem)
urlComponents.queryItems = queryItems
return urlComponents.url!
}
}