Swift 4 和 Swift 3 语法上的一些些区别

字体设置

在 Swift 3 中 NSFontAttributeName, NSForegroundColorAttributeName 这俩关键字在 Swift 4 中齐刷刷报错, 报的错还都差不多 Use of unresolved identifier 'NSXXXAttributeName'
其实, 在 Swift 4 中的正确打开方式应该是
NSAttributedStringKey.font, NSAttributedStringKey.foregroundColor

举个 🌰
Swift 4:
xx.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.orange], for: .highlighted)
Swift 3:
xx.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orange], for: .highlighted)

附录完整版:

extension NSAttributedStringKey {

    @available(iOS 6.0, *)
    public static let font: NSAttributedStringKey // UIFont, default Helvetica(Neue) 12

    @available(iOS 6.0, *)
    public static let paragraphStyle: NSAttributedStringKey // NSParagraphStyle, default defaultParagraphStyle

    @available(iOS 6.0, *)
    public static let foregroundColor: NSAttributedStringKey // UIColor, default blackColor

    @available(iOS 6.0, *)
    public static let backgroundColor: NSAttributedStringKey // UIColor, default nil: no background

    @available(iOS 6.0, *)
    public static let ligature: NSAttributedStringKey // NSNumber containing integer, default 1: default ligatures, 0: no ligatures

    @available(iOS 6.0, *)
    public static let kern: NSAttributedStringKey // NSNumber containing floating point value, in points; amount to modify default kerning. 0 means kerning is disabled.

    @available(iOS 6.0, *)
    public static let strikethroughStyle: NSAttributedStringKey // NSNumber containing integer, default 0: no strikethrough

    @available(iOS 6.0, *)
    public static let underlineStyle: NSAttributedStringKey // NSNumber containing integer, default 0: no underline

    @available(iOS 6.0, *)
    public static let strokeColor: NSAttributedStringKey // UIColor, default nil: same as foreground color

    @available(iOS 6.0, *)
    public static let strokeWidth: NSAttributedStringKey // NSNumber containing floating point value, in percent of font point size, default 0: no stroke; positive for stroke alone, negative for stroke and fill (a typical value for outlined text would be 3.0)

    @available(iOS 6.0, *)
    public static let shadow: NSAttributedStringKey // NSShadow, default nil: no shadow

    @available(iOS 7.0, *)
    public static let textEffect: NSAttributedStringKey // NSString, default nil: no text effect

    
    @available(iOS 7.0, *)
    public static let attachment: NSAttributedStringKey // NSTextAttachment, default nil

    @available(iOS 7.0, *)
    public static let link: NSAttributedStringKey // NSURL (preferred) or NSString

    @available(iOS 7.0, *)
    public static let baselineOffset: NSAttributedStringKey // NSNumber containing floating point value, in points; offset from baseline, default 0

    @available(iOS 7.0, *)
    public static let underlineColor: NSAttributedStringKey // UIColor, default nil: same as foreground color

    @available(iOS 7.0, *)
    public static let strikethroughColor: NSAttributedStringKey // UIColor, default nil: same as foreground color

    @available(iOS 7.0, *)
    public static let obliqueness: NSAttributedStringKey // NSNumber containing floating point value; skew to be applied to glyphs, default 0: no skew

    @available(iOS 7.0, *)
    public static let expansion: NSAttributedStringKey // NSNumber containing floating point value; log of expansion factor to be applied to glyphs, default 0: no expansion

    
    @available(iOS 7.0, *)
    public static let writingDirection: NSAttributedStringKey // NSArray of NSNumbers representing the nested levels of writing direction overrides as defined by Unicode LRE, RLE, LRO, and RLO characters.  The control characters can be obtained by masking NSWritingDirection and NSWritingDirectionFormatType values.  LRE: NSWritingDirectionLeftToRight|NSWritingDirectionEmbedding, RLE: NSWritingDirectionRightToLeft|NSWritingDirectionEmbedding, LRO: NSWritingDirectionLeftToRight|NSWritingDirectionOverride, RLO: NSWritingDirectionRightToLeft|NSWritingDirectionOverride,

    
    @available(iOS 6.0, *)
    public static let verticalGlyphForm: NSAttributedStringKey // An NSNumber containing an integer value.  0 means horizontal text.  1 indicates vertical text.  If not specified, it could follow higher-level vertical orientation settings.  Currently on iOS, it's always horizontal.  The behavior for any other value is undefined.
}

后续补充。。。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容