这是一个库,用于向各种 Foundation 类添加下标支持。它还为 NSString、NSAttributedString 及其属性的工作提供了某些有趣解决方案。
以下 Foundation 类已被扩展。
NSArray
NSOrderedSet
NSMutableOrderedSet
NSMapTable
NSString
NSMutableString
NSAttributedString
NSMutableAttributedString
NSOperationQueue
除了标准的下标选项外,NSAttributed 字符串现在通过下标提供了额外的便捷性。
我提供了一种简单的方式来生成 NSAtributedString
NSMutableAttributedString *string = $("Attributed String").mutableCopy;
NSString *subString = [aString attributedSubstringFromRange:NSMakeRange(0, 10)];
// can now be replaced with
NSString *subString = aString[[@0:10]];
您甚至可以使用赋值来替换一个范围
NSMutableAttributedString *string = $("a");
[string replaceCharactersInRange:NSMakeRange(0, 1) withAttributedString:[NSAttributedString attributedStringWithString:@"b"]];
// can now be replaced with
string[[@0:1]] = @"b";
// if you want to replace part of a string ((or entirely from a given index), this is even simpler:
string[@0] = @"b";
请注意,现在在大多数情况下,NSAttributedString 和 NSString 可以互换使用。
那么属性呢?NSAttributedString 现在有了很好的属性属性,允许您对属性应用下标;
// If you need to replace attributes from a specific index
string.attributes[0] = attributes;
// If you need to add a specific attribute to the entire string
string.attributes[NSKernAttributeName] = @2;
// Thanks for the nicer syntax, you can even add a specific attribute to a range too (add 2.0 kerning to the range {0, 10}
string.attributes[[@0:10:NSKernAttributeName]] = @2;
// Finally you can apply a series of attribuets to a range
string.attributes[[@0:10]] = attributes;
要运行示例项目,请克隆该仓库并运行项目。
SPXSubscripting 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中:
pod 'SPXSubscripting'
SPXSubscripting 根据 MIT 许可证提供。有关更多信息,请参阅 LICENSE 文件。