第一阶段:git开源工具:PinYin4Objc
1
2
3
4
5
6
|
-
(
NSString
*
)
transformToPinyin
{
NSMutableString
*mutableString
=
[
NSMutableString
stringWithString
:self
]
;
CFStringTransform
(
(
CFMutableStringRef
)
mutableString
,
NULL
,
kCFStringTransformToLatin
,
false
)
;
CFStringTransform
(
(
CFMutableStringRef
)
mutableString
,
NULL
,
kCFStringTransformStripDiacritics
,
false
)
;
return
mutableString
;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
-
(
NSString
*
)
transformToPinyin
{
NSMutableString
*mutableString
=
[
NSMutableString
stringWithString
:self
]
;
BOOL
isNeedTransform
=
!
[
self
isAllEngNumAndSpecialSign
]
;
if
(
isNeedTransform
)
{
CFStringTransform
(
(
CFMutableStringRef
)
mutableString
,
NULL
,
kCFStringTransformToLatin
,
false
)
;
CFStringTransform
(
(
CFMutableStringRef
)
mutableString
,
NULL
,
kCFStringTransformStripDiacritics
,
false
)
;
}
return
mutableString
;
}
-
(
BOOL
)
isAllEngNumAndSpecialSign
{
NSString
*regularString
=
@"^[A-Za-z0-9\\p{Z}\\p{P}]+$"
;
NSPredicate
*predicate
=
[
NSPredicate
predicateWithFormat
:
@"SELF MATCHES %@"
,
regularString
]
;
return
[
predicate
evaluateWithObject
:self
]
;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
-
(
NSString
*
)
transformToPinyin
{
NSMutableString
*mutableString
=
[
NSMutableString
stringWithString
:self
]
;
BOOL
isNeedTransform
=
!
[
self
isAllEngNumAndSpecialSign
]
;
if
(
isNeedTransform
)
{
CFStringTransform
(
(
CFMutableStringRef
)
mutableString
,
NULL
,
kCFStringTransformToLatin
,
false
)
;
CFStringTransform
(
(
CFMutableStringRef
)
mutableString
,
NULL
,
kCFStringTransformStripCombiningMarks
,
false
)
;
}
return
mutableString
;
}
-
(
BOOL
)
isAllEngNumAndSpecialSign
{
NSString
*regularString
=
@"^[A-Za-z0-9\\p{Z}\\p{P}]+$"
;
NSPredicate
*predicate
=
[
NSPredicate
predicateWithFormat
:
@"SELF MATCHES %@"
,
regularString
]
;
return
[
predicate
evaluateWithObject
:self
]
;
}
|
1
2
3
4
5
6
|
-
(
NSString
*
)
transformToPinyin
{
NSMutableString
*mutableString
=
[
NSMutableString
stringWithString
:self
]
;
CFStringTransform
(
(
CFMutableStringRef
)
mutableString
,
NULL
,
kCFStringTransformToLatin
,
false
)
;
mutableString
=
(
NSMutableString
*
)
[
mutableString
stringByFoldingWithOptions
:NSDiacriticInsensitiveSearch
locale
:
[
NSLocale
currentLocale
]
]
;
return
mutableString
;
}
|
Demo移步https://github.com/panghaijiao/PHJTransformPinyinDemo,喜欢请点赞,感谢你的来访!
该文章出自:庞海礁的个人空间
地址:http://www.olinone.com/?p=131