美文网首页
Swift 4 和 Swift 3 语法上的一些些区别

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

作者: biubiu15 | 来源:发表于2018-08-01 14:22 被阅读0次

    字体设置

    在 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.
    }
    

    后续补充。。。

    相关文章

      网友评论

          本文标题:Swift 4 和 Swift 3 语法上的一些些区别

          本文链接:https://www.haomeiwen.com/subject/xbwlvftx.html