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 enum拡張機能を使用してください。
" 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 )これらの新しい酵素と、 NSRange代わりにSwiftのRangeタイプを使用するために、いくつかの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ライセンスの下で利用できます。詳細については、ライセンスファイルを参照してください。