Reputation: 197
UIFontDescriptor *bodyFontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
UIFontDescriptor *italicBoldDescriptor = [bodyFontDescriptor fontDescriptorByAddingAttributes:@{UIFontDescriptorTraitsAttribute : @{ UIFontSymbolicTrait: @(UIFontDescriptorTraitItalic | UIFontDescriptorTraitBold)}}];
UIFont *comboFont = [UIFont fontWithDescriptor:italicBoldDescriptor size:0.0];
[self.body.textStorage addAttribute:NSFontAttributeName value:comboFont range:self.body.selectedRange];
My goal was to bold/italic selected text in a text view. After doing a bit of research, this is what I have and it works! However, I really do not understand the code, especially the second line. If someone could explain exactly what this code is doing I would greatly appreciate it. Also I do not understand the dictionary syntax that is happening in the second line. What is the syntax with the '|' character? I have never seen that before. Thank you very much for your time.
Upvotes: 1
Views: 780
Reputation: 17969
The "|" operator is the C bit-OR which is combining two flag values into a single number.
Apple's doc for bold shows it as UIFontDescriptorTraitBold = 1u << 1
and for italic as UIFontDescriptorTraitItalic = 1u << 0
.
So that OR clause is combining the binary values 0b00000010 and 0b00000001 to make a single flag 0b00000011.
Upvotes: 1
Reputation: 745
UIFontDescriptorTraitsAttribute
, An NSDictionary instance instance fully describing font traits. The default value is supplied by the font.
UIFontDescriptorSymbolicTraits
symbolically describes stylistic aspects of a font.
You can check iOS Developer library
Upvotes: 2