我在UITextView中有
一个NSAttributed
String,并且
在处理动态类型,特别是文本样式时,希望处理UIContentSizeCategoryDidChangeNotification.我所看到的所有例子(IntroToTextKitDemo)
解决了整个UI元素的字体是一样的.有没有人知道如何正确处理这些所有
属性更新正确?
注意:当iOS 7处于NDA下时,我在开发者论坛上询问了这一点.我在这里发布,因为我找到一个解决方案,并认为其他人@R_754_@R_673_10098@@它有用.
我找到了
一个解决方案.处理
通知时,您需要处理
属性并查找文本样式并更新字体:
@H_
607_8@- (void)preferredContentSizeChanged:(N
snotification *)aNotification
{
UITextView *textView = <the text view holding your attributed text>
NSMutableAttributed
String *attributed
String = [[NSMutableAttributed
String alloc] initWithAttributed
String:textView.attributedText];
NSRange range = N
smakeRange(0,attributed
String.length - 1
);
// Walk the
String's attributes
[attributed
String enumerateAttributesInRange
:range options:NSAttributed
StringEnumerationReverse usingBlock:
^(NS
Dictionary *attributes,NSRange range,BOOL *sto
p) {
// Find the font descriptor which is based on the old font size change
NSMutable
Dictionary *mutableAttributes = [NSMutable
Dictionary
DictionaryWith
Dictionary: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 ob
jectForKey: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];
[attributed
String addAttribute:NSFontAttribut
ENAME value:newFont range
:range];
}];
textView.attributedText = attributed
String;
}