iOS 開發(fā)小記-01

最近又開始寫不少業(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!
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容