You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
请注意,这个方法的加入相对晚一些(在 OS X 10.6 和 iOS 4.0 的时候)。在之前,按字符循环一个字符串要麻烦得多。
NSString *s = @"The weather on \U0001F30D is \U0001F31E today.";
// The weather on 🌍 is 🌞 today. NSRange fullRange = NSMakeRange(0, [s length]);
[s enumerateSubstringsInRange:fullRange
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop)
{
NSLog(@"%@%@", substring, NSStringFromRange(substringRange));
}];
富文本字符连接处理(自动断字)
TextKit的富文本属性
\*
Hyphenation is attempted when the ratio of the text width (as broken without hyphenation) to the width of the line fragment is less than the hyphenation factor.When the paragraph’s hyphenation factor is 0.0, the layout manager’s hyphenation factor is used instead.When both are 0.0, hyphenation is disabled.This property detects the user-selected language by examining the first item in preferredLanguages
*\
iOS 中非常简单,TextKit 提供了良好的自动断字支持,可以通过设置 hyphenationFactor 属性非常方便的实现自动断字:
letparagraphStyle=NSMutableParagraphStyle()
paragraphStyle.hyphenationFactor =1
paragraphStyle.alignment =.center
#import "NSLocale+ForceHyphenation.h"@implementation NSLocale (ForceHyphenation)+(void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SEL originalSelector =@selector(preferredLanguages);
SEL swizzledSelector =@selector(sr_preferredLanguages);
Class class= object_getClass((id)self);
Method originalMethod =class_getClassMethod(class, originalSelector);
Method swizzledMethod =class_getClassMethod(class, swizzledSelector);
BOOL didAddMethod =class_addMethod(class,
originalSelector,method_getImplementation(swizzledMethod),method_getTypeEncoding(swizzledMethod));
if (didAddMethod){class_replaceMethod(class,
swizzledSelector,method_getImplementation(originalMethod),method_getTypeEncoding(originalMethod));
}else{method_exchangeImplementations(originalMethod, swizzledMethod);
}});
}+(NSArray <NSString *>*)sr_preferredLanguages {[self sr_preferredLanguages];
return @[ @"en"];
}@end
利用YYText设置truncationToken可以完美的实现这项功能,而且效率比这个高
扩展处理的简单方法
#import"NSString+SoftHyphenation.h"unicharconstkTextDrawingSoftHyphenUniChar = 0x00AD;
NSString * constkTextDrawingSoftHyphenToken = @"\u00ad"; // NOTE: UTF-8 soft hyphen!NSString * const NSStringSoftHyphenationErrorDomain = @"NSStringSoftHyphenationErrorDomain";
@implementationNSString (SoftHyphenation)
- (NSError *)hyphen_createOnlyError
{
NSDictionary *userInfo = @{
NSLocalizedDescriptionKey: @"Hyphenation is not available for given locale",
NSLocalizedFailureReasonErrorKey: @"Hyphenation is not available for given locale",
NSLocalizedRecoverySuggestionErrorKey: @"You could try using a different locale even though it might not be 100% correct"
};
return [NSErrorerrorWithDomain:NSStringSoftHyphenationErrorDomain code:NSStringSoftHyphenationErrorNotAvailableForLocale userInfo:userInfo];
}
- (NSString *)softHyphenatedStringWithLocale:(NSLocale *)localeerror:(outNSError **)error
{
CFLocaleRef localeRef = (__bridge CFLocaleRef)(locale);
if(!CFStringIsHyphenationAvailableForLocale(localeRef))
{
if(error != NULL)
{
*error = [selfhyphen_createOnlyError];
}
return [selfcopy];
}
else
{
NSMutableString *string = [selfmutableCopy];
unsignedchar hyphenationLocations[string.length];
memset(hyphenationLocations, 0, string.length);
CFRange range = CFRangeMake(0, string.length);
for(int i = 0; i < string.length; i++)
{
CFIndex location = CFStringGetHyphenationLocationBeforeIndex((CFStringRef)string,
i,
range,
0,
localeRef,
NULL);
if(location >= 0 && location < string.length)
{
hyphenationLocations[location] = 1;
}
}
for(unsignedlong i = string.length - 1; i > 0; i--)
{
if(hyphenationLocations[i])
{
[string insertString:kTextDrawingSoftHyphenTokenatIndex:i];
}
}
if(error != NULL) { *error = nil;}
return string;
}
}
- (NSString *)softHyphenatedString
{
NSLocale *locale = [NSLocalelocaleWithLocaleIdentifier:@"en_US"];
return [selfsoftHyphenatedStringWithLocale:locale error:nil];
}
@end