SwiftyAttributes
Result Builder + Bug
使用SwiftyAttributes ,您可以創建類似的屬性字符串:
let fancyString = " Hello World! " . withTextColor ( . blue ) . withUnderlineStyle ( . styleSingle )或者,使用Attribute枚舉:
let fancyString = " Hello World! " . withAttributes ( [
. backgroundColor ( . magenta ) ,
. strokeColor ( . orange ) ,
. strokeWidth ( 1 ) ,
. baselineOffset ( 5.2 )
] )您還可以使用加號輕鬆將屬性字符串組合在一起:
let fancyString = " Hello " . withFont ( . systemFont ( ofSize : 12 ) ) + " World! " . withFont ( . systemFont ( ofSize : 18 ) )SwiftyAttributes可以支持可可和可可觸摸中可用的每個屬性。
dependencies: [
. package ( url : " https://github.com/eddiekaiger/SwiftyAttributes.git " , from : " 5.3.0 " )
] pod 'SwiftyAttributes'
github "eddiekaiger/SwiftyAttributes"
在SwiftyAttributes中初始化歸因的字符串可以做幾種方法:
使用with[Attribute]擴展:
" Hello World " . withUnderlineColor ( . red ) . withUnderlineStyle ( . styleDouble )使用Attribute枚舉擴展:
" Hello World " . withAttributes ( [ . underlineColor ( . red ) , . underlineStyle ( . styleDouble ) ] )使用初始化器中的Attribute枚舉:
NSAttributedString ( string : " Hello World " , swiftyAttributes : [ . kern ( 5 ) , . backgroundColor ( . gray ) ] )您可以使用內置的NSAttributedString.Key Enum:
let attr : Attribute ? = myAttributedString . swiftyAttribute ( . shadow , at : 5 )提供了幾種使用這些新枚舉以及Swift Range類型而不是NSRange的API方法。一些方法簽名包括:
extension NSMutableAttributedString {
func addAttributes ( _ attributes : [ Attribute ] , range : Range < Int > )
func addAttributes ( _ attributes : [ Attribute ] , range : NSRange )
func setAttributes ( _ attributes : [ Attribute ] , range : Range < Int > )
func setAttributes ( _ attributes : [ Attribute ] , range : NSRange )
func replaceCharacters ( in range : Range < Int > , with str : String )
func replaceCharacters ( in range : Range < Int > , with attrString : NSAttributedString )
func deleteCharacters ( in range : Range < Int > )
func removeAttribute ( _ name : NSAttributedStringKey , range : Range < Int > )
}
extension NSAttributedString {
convenience init ( string str : String , swiftyAttributes : [ Attribute ] )
func withAttributes ( _ attributes : [ Attribute ] ) -> NSMutableAttributedString
func withAttribute ( _ attribute : Attribute ) -> NSMutableAttributedString
func attributedSubstring ( from range : Range < Int > ) -> NSAttributedString
func swiftyAttribute ( _ attrName : NSAttributedStringKey , at location : Int , effectiveRange range : NSRangePointer ? = nil ) -> Attribute ?
func swiftyAttributes ( in range : Range < Int > , options : NSAttributedString . EnumerationOptions = [ ] ) -> [ ( [ Attribute ] , Range < Int > ) ]
func enum erateSwiftyAttributes ( in enum erationRange : Range < Int > , options : NSAttributedString . EnumerationOptions = [ ] , using block : ( _ attrs : [ Attribute ] , _ range : Range < Int > , _ stop : UnsafeMutablePointer < ObjCBool > ) -> Void )
func enum erateSwiftyAttribute ( _ attrName : NSAttributedStringKey , in enum erationRange : Range < Int > , options : NSAttributedString . EnumerationOptions = [ ] , using block : ( _ value : Any ? , _ range : Range < Int > , _ stop : UnsafeMutablePointer < ObjCBool > ) -> Void )
}
extension String {
var attributedString : NSMutableAttributedString
func withAttributes ( _ attributes : [ Attribute ] ) -> NSMutableAttributedString
func withAttribute ( _ attribute : Attribute ) -> NSMutableAttributedString
}
// ... and more!有關問題,支持和建議,請打開一個問題。
SwiftyAttributes可根據MIT許可獲得。有關更多信息,請參見許可證文件。