iOS 成长快线

iOS 开发小记-01

2019-12-08  本文已影响0人  hanson21

最近又开始写不少业务代码了,有些小知识点小坑,用这个系列记录一下。
iOS 开发小记-01
iOS 开发小记-02

1. UITextView 修改部分字体颜色,换行垂直居中

// 使用 \n 来换行
let attributeString = NSMutableAttributedString(string: "测试文字测试文字测试文字测试文字,高亮文字高亮文字高亮文字,\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
// 注意这个 textAligment 需要在 attributedText 赋值后调用,不然不生效
textView.textAlignment = .center

注意:UITextView 居中(textAlignment) 不生效问题

image.png

2. 应用内禁用 DarkMode

Info.plist 中添加 User Interface Style : Light,即可在应用内禁用暗黑模式。

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 添加参数的方法

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!
    }
}
上一篇 下一篇

猜你喜欢

热点阅读