SwiftyAttributes
Result Builder + Bug
sfiftyattributes를 사용하면 다음과 같은 문자열을 만들 수 있습니다.
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 ) )Sfiftyattributes는 코코아 및 코코아 터치로 제공되는 모든 속성을 지원합니다.
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 ) 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!질문, 지원 및 제안은 문제를 열어주십시오.
Sfiftyattributes는 MIT 라이센스에 따라 제공됩니다. 자세한 내용은 라이센스 파일을 참조하십시오.