Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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
Ios7 在UITextView中处理NSAttributedString的UIContentSizeCategoryDidChangeNotification_Ios7_Nsattributedstring_Dynamic Text_Text Styling - Fatal编程技术网

Ios7 在UITextView中处理NSAttributedString的UIContentSizeCategoryDidChangeNotification

Ios7 在UITextView中处理NSAttributedString的UIContentSizeCategoryDidChangeNotification,ios7,nsattributedstring,dynamic-text,text-styling,Ios7,Nsattributedstring,Dynamic Text,Text Styling,我在UITextView中有一个NSAttributedString,在处理动态类型,特别是文本样式时,我希望处理UIContentSizeCategoryDidChangeNotification。我看到的所有示例(IntroToTextKitDemo)都说明了整个UI元素的字体相同的情况。是否有人知道如何正确处理此问题,以便所有属性都能正确更新 注意:当iOS 7处于NDA状态时,我在开发者论坛上问过这个问题。我把它贴在这里是因为我找到了一个解决方案,并认为其他人可能会觉得它有用。我找到了一

我在UITextView中有一个NSAttributedString,在处理动态类型,特别是文本样式时,我希望处理UIContentSizeCategoryDidChangeNotification。我看到的所有示例(IntroToTextKitDemo)都说明了整个UI元素的字体相同的情况。是否有人知道如何正确处理此问题,以便所有属性都能正确更新


注意:当iOS 7处于NDA状态时,我在开发者论坛上问过这个问题。我把它贴在这里是因为我找到了一个解决方案,并认为其他人可能会觉得它有用。

我找到了一个解决方案。处理通知时,您需要遍历属性,查找文本样式并更新字体:

- (void)preferredContentSizeChanged:(NSNotification *)aNotification
{
    UITextView *textView = <the text view holding your attributed text>

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:textView.attributedText];
    NSRange range = NSMakeRange(0, attributedString.length - 1);

    // Walk the string's attributes
    [attributedString enumerateAttributesInRange:range options:NSAttributedStringEnumerationReverse usingBlock:
     ^(NSDictionary *attributes, NSRange range, BOOL *stop) {

         // Find the font descriptor which is based on the old font size change
         NSMutableDictionary *mutableAttributes = [NSMutableDictionary dictionaryWithDictionary:attributes];
         UIFont *font = mutableAttributes[@"NSFont"];
         UIFontDescriptor *fontDescriptor = font.fontDescriptor;

         // Get the text style and get a new font descriptor based on the style and update font size
         id styleAttribute = [fontDescriptor objectForKey:UIFontDescriptorTextStyleAttribute];
         UIFontDescriptor *newFontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:styleAttribute];

         // Get the new font from the new font descriptor and update the font attribute over the range
         UIFont *newFont = [UIFont fontWithDescriptor:newFontDescriptor size:0.0];
         [attributedString addAttribute:NSFontAttributeName value:newFont range:range];
     }];

    textView.attributedText = attributedString;
}
-(void)首选内容大小更改:(NSNotification*)通知
{
UITextView*文本视图=
NSMutableAttributedString*attributedString=[[NSMutableAttributedString alloc]initWithAttributedString:textView.attributedText];
NSRange range=NSMakeRange(0,attributeString.length-1);
//遍历字符串的属性
[attributedString枚举AttributesInRange:范围选项:NSAttributedStringEnumerationReverse usingBlock:
^(NSDictionary*属性、NSRange范围、BOOL*停止){
//查找基于旧字体大小更改的字体描述符
NSMutableDictionary*mutableAttributes=[NSMutableDictionary Dictionary WithDictionary:attributes];
UIFont*font=mutableAttributes[@“NSFont”];
UIFontDescriptor*fontDescriptor=font.fontDescriptor;
//获取文本样式,并根据样式获取新的字体描述符,并更新字体大小
id styleAttribute=[fontDescriptor objectForKey:UIFontDescriptorTextStyleAttribute];
UIFontDescriptor*newFontDescriptor=[UIFontDescriptor PreferredFontDescriptor WithTextStyle:styleAttribute];
//从新字体描述符获取新字体,并更新范围内的字体属性
UIFont*newFont=[UIFont fontWithDescriptor:newFontDescriptor大小:0.0];
[attributedString addAttribute:NSFontAttributeName值:新字体范围:范围];
}];
textView.attributedText=attributedString;
}

谢谢,非常方便。方法的第三行应该是
NSRange range=NSMakeRange(0,attributedString.length)以覆盖整个文本。