مع 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 المدمج:
let attr : Attribute ? = myAttributedString . swiftyAttribute ( . shadow , at : 5 ) يتم توفير العديد من أساليب API لاستخدام هذه التعدادات الجديدة بالإضافة إلى نوع Range SWIFT بدلاً من NSRange . تشمل بعض توقيعات الطريقة:
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 متاح تحت رخصة معهد ماساتشوستس للتكنولوجيا. انظر ملف الترخيص لمزيد من المعلومات.