Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 如何将NSAttributedString中的字符大小写更改为大写_Cocoa_Character_Uppercase_Nsattributedstring - Fatal编程技术网

Cocoa 如何将NSAttributedString中的字符大小写更改为大写

Cocoa 如何将NSAttributedString中的字符大小写更改为大写,cocoa,character,uppercase,nsattributedstring,Cocoa,Character,Uppercase,Nsattributedstring,我想将包含RTFD的NSAttributedString转换为大写,而不会丢失现有字符和图形的属性 谢谢,编辑: @fluidsonic是正确的,因为原始代码不正确。下面是Swift中的更新版本,它将每个属性范围中的文本替换为该范围中字符串的大写版本 extension NSAttributedString { func uppercased() -> NSAttributedString { let result = NSMutableAttributedStr

我想将包含RTFD的NSAttributedString转换为大写,而不会丢失现有字符和图形的属性

谢谢,

编辑:

@fluidsonic是正确的,因为原始代码不正确。下面是Swift中的更新版本,它将每个属性范围中的文本替换为该范围中字符串的大写版本

extension NSAttributedString {
    func uppercased() -> NSAttributedString {

        let result = NSMutableAttributedString(attributedString: self)

        result.enumerateAttributes(in: NSRange(location: 0, length: length), options: []) {_, range, _ in
            result.replaceCharacters(in: range, with: (string as NSString).substring(with: range).uppercased())
        }

        return result
    }
}
原始答复:

它的作用是:

  • 创建输入属性字符串的可变副本
  • 从该字符串中获取所有属性并将它们放入数组中,以便以后使用
  • 使用内置的
    NSString
    方法生成大写纯字符串
  • 重新应用所有属性

  • 代码不正确。它可能会生成属性字符串,其中属性被关闭了几个字符,甚至可能崩溃。该方法取决于初始字符串和大写字符串是否具有相同的字符数。但根据“Case变换不能保证对称,也不能产生与原始字符串长度相同的字符串”。
    - (NSAttributedString *)upperCaseAttributedStringFromAttributedString:(NSAttributedString *)inAttrString {
        // Make a mutable copy of your input string
        NSMutableAttributedString *attrString = [inAttrString mutableCopy];
    
        // Make an array to save the attributes in
        NSMutableArray *attributes = [NSMutableArray array];
    
        // Add each set of attributes to the array in a dictionary containing the attributes and range
        [attrString enumerateAttributesInRange:NSMakeRange(0, [attrString length]) options:0 usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) {
            [attributes addObject:@{@"attrs":attrs, @"range":[NSValue valueWithRange:range]}];
        }];
    
        // Make a plain uppercase string
        NSString *string = [[attrString string]uppercaseString];
    
        // Replace the characters with the uppercase ones
        [attrString replaceCharactersInRange:NSMakeRange(0, [attrString length]) withString:string];
    
        // Reapply each attribute
        for (NSDictionary *attribute in attributes) {
            [attrString setAttributes:attribute[@"attrs"] range:[attribute[@"range"] rangeValue]];
        }
    
        return attrString;
    }