首页 > 代码库 > <iOS小技巧> 昵称格式判断
<iOS小技巧> 昵称格式判断
一、使用方式
+ 如下代码块功能:判断字体,判断字体输入格式
NSString *firstStr = [name substringToIndex:1];
NSArray *num = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
if ([num containsObject:firstStr]) {
[self warnUser:@"昵称不能以数字开头"];
return;
}
if ([name containsStringAvailable:@" "]) {
[self warnUser:@"昵称不可以包含空格"];
return;
}
if ([StringUtil stringContainsEmoji:name]) {
[self warnUser:@"昵称不可以包含表情等特殊字符"];
return;
}
if ([StringUtil lengthByUTF8:name] > 24) {
[self warnUser:@"昵称过长, 汉字最多8个, 英文最多24个"];
return;
}
NSArray *num = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
if ([num containsObject:firstStr]) {
[self warnUser:@"昵称不能以数字开头"];
return;
}
if ([name containsStringAvailable:@" "]) {
[self warnUser:@"昵称不可以包含空格"];
return;
}
if ([StringUtil stringContainsEmoji:name]) {
[self warnUser:@"昵称不可以包含表情等特殊字符"];
return;
}
if ([StringUtil lengthByUTF8:name] > 24) {
[self warnUser:@"昵称过长, 汉字最多8个, 英文最多24个"];
return;
}
+ containsStringAvailable() 方法:用来判断是否包含此字符
- (BOOL)containsStringAvailable:(NSString *)other
{
if ([selfisIOS8AndLater]) {
return [self containsString:other];
}
else {
NSRange range = [self rangeOfString:other];
return range.length != 0;
}
}
{
if ([selfisIOS8AndLater]) {
return [self containsString:other];
}
else {
NSRange range = [self rangeOfString:other];
return range.length != 0;
}
}
- (BOOL)isIOS8AndLater
{
return [[[UIDevicecurrentDevice] systemVersion] floatValue] >= 8.0;
}
{
return [[[UIDevicecurrentDevice] systemVersion] floatValue] >= 8.0;
}
+ stringContainsEmoji() 方法:用来判断是否包含表情字符
+ (BOOL)stringContainsEmoji:(NSString *)string
{
__blockBOOL returnValue = http://www.mamicode.com/NO;
NSRange range = NSMakeRange(0, [string length]);
[string enumerateSubstringsInRange:range options:NSStringEnumerationByComposedCharacterSequencesusingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
constunichar high = [substring characterAtIndex: 0];
// Surrogate pair (U+1D000-1F9FF)
if (0xD800 <= high && high <= 0xDBFF) {
constunichar low = [substring characterAtIndex: 1];
constint codepoint = ((high - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
if (0x1D000 <= codepoint && codepoint <= 0x1F9FF){
returnValue = http://www.mamicode.com/YES;
}
}
// Not surrogate pair (U+2100-27BF)
else {
if (0x2100 <= high && high <= 0x27BF){
returnValue = http://www.mamicode.com/YES;
}
}
}];
return returnValue;
}
{
__blockBOOL returnValue = http://www.mamicode.com/NO;
NSRange range = NSMakeRange(0, [string length]);
[string enumerateSubstringsInRange:range options:NSStringEnumerationByComposedCharacterSequencesusingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
constunichar high = [substring characterAtIndex: 0];
// Surrogate pair (U+1D000-1F9FF)
if (0xD800 <= high && high <= 0xDBFF) {
constunichar low = [substring characterAtIndex: 1];
constint codepoint = ((high - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
if (0x1D000 <= codepoint && codepoint <= 0x1F9FF){
returnValue = http://www.mamicode.com/YES;
}
}
// Not surrogate pair (U+2100-27BF)
else {
if (0x2100 <= high && high <= 0x27BF){
returnValue = http://www.mamicode.com/YES;
}
}
}];
return returnValue;
}
+ lengthByUTF8() 方法:用来判断 UTF-8 字符长度(一个汉字 = 3个 UTF-8 字符)
+ (int)lengthByUTF8:(NSString *)str
{
int strlength = 0;
char *p = (char *)[str cStringUsingEncoding:NSUTF8StringEncoding];
for (int i=0; i<[str lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; i++) {
if (*p) {
p++;
strlength++;
}
else {
p++;
}
}
return strlength;
}
{
int strlength = 0;
char *p = (char *)[str cStringUsingEncoding:NSUTF8StringEncoding];
for (int i=0; i<[str lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; i++) {
if (*p) {
p++;
strlength++;
}
else {
p++;
}
}
return strlength;
}
二、我的想法
明天要出个String 的工具类出来,分别讲解一下string需要的方法,今天在判断UTF-8字符长度的时候,判断错了,把一个汉字作为两个长度处理了,导致测试测出Bug,这些小问题一定要自我验证,不能到测试那里才出现这种问题。
三、思考与行动
1.如何通过一个方法来判断汉字最多10个,英文最多5个?
2.判断是否包含表情字符的方法,我设计的合理么?是否会存在一些问题?你有没有更简洁的方法?
<iOS小技巧> 昵称格式判断
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。