Find attributes from user attribute string

In my application, the User enters whatever he wants, and he can make the text Bold, Italic and Underline.

I can make the text bold, italic and underline.

But I need to find which text (or character) is Bold, italics and underline from the attribute string that the user typed .

Any types of links, tutorial, code will be of great help.

EDIT:

I need a result like this ...

if character 6 is italic, then type in italics.
if character 14 is in bold, print in bold.

I need to convert all text from the Calibri font family to the HelveticaNeue font family without changing its attribute.

+6
ios objective-c ios7 nsattributedstring
Apr 18 '14 at 11:25
source share
1 answer

You can list Attributes with this code (entire range).

[attrStr enumerateAttributesInRange:NSMakeRange(0, [attrStr length]) options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock: ^(NSDictionary *attributes, NSRange range, BOOL *stop) { //Do something here } }]; 

For a specific range, you need to change the NSMakeRange(0, [attrStr lenght]) code NSMakeRange(0, [attrStr lenght]) to the desired range.

After your editing, your specific need, this code should do the work:

 [attrStr enumerateAttributesInRange:NSMakeRange(0, [attrStr length]) options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock: ^(NSDictionary *attributes, NSRange range, BOOL *stop) { NSMutableDictionary *mutableAttributes = [NSMutableDictionary dictionaryWithDictionary:attributes]; UIFont *currentFont = [mutableAttributes objectForKey: NSFontAttributeName]; UIFont *newFont; if ([[currentFont fontName] isEqualToString:@"Calibri-BoldItalic"]) newFont = [UIFont fontWithName:@"HelveticaNeue-BoldItalic" size:currentFont.pointSize]; else if ([[currentFont fontName] isEqualToString:@"Calibri-Bold"]) newFont = [UIFont fontWithName:@"HelveticaNeue-Bold" size:currentFont.pointSize]; else if ([[currentFont fontName] isEqualToString:@"Calibri-Italic"]) newFont = [UIFont fontWithName:@"HelveticaNeue-Italic" size:currentFont.pointSize]; [mutableAttributes setObject:newFont forKey:@"NSFont"]; [attrStr setAttributes:mutableAttributes range:range]; }]; 

From your comments, you can see more complex things, so I will give you some ideas / tips for working / checking. You just need to fill out my previous code to manage all things.
• Case: no font.
You just need to check if currentFont . If so, you install what you want. Since the attribute for italics / bold is missing (since they are included in the font), I suggest you set for newFont : HelveticaNeue.
• Case: This is a different font than Caliber. There it is quite difficult.
I worked with this, and here is what I got: The font name (which is in the postscript name ) is made in the same way for HelveticaNeue (as an example):

Helveticaneue
Helvetica neue-light
HelveticaNeue-UltraLight
HelveticaNeue-Medium
HelveticaNeue-LightItalic
HelveticaNeue-Italic
HelveticaNeue-UltraLightItalic
Helvetica neue-bold
HelveticaNeue-BoldItalic
HelveticaNeue-CondensedBold
HelveticaNeue-CondensedBlack

So, the first thing you might think: I just need to check the name suffix (with hasSuffix: . But this may go wrong. Indeed, some fonts have a foundry suffix (the ones I found are LT, MT, and EF).
Well, for example:
Times New Roman "Real Name" - TimesNewRomanPSMT.
And its cursive version of TimesNewRomanPS-ItalicMT
Therefore, we need to check if it has a foundry suffix, because TimesNewRomanPSMT-Italic will not work (since it does not exist, if you try to install a font with this name, it will be zero). So, as far as I know, and as far as I can have some experience and what I saw, what you can do: Check if there is [currentFont name] : BoldItalic , Italic or Bold using NSRegularExpression or rangeOfString: Please note that I suggest checking earlier for BoldItalic , since Italic can “hide” Bold directly in front of it, and Bold can “hide” Italic right after it.
For this, I would suggest creating such a method (or this style):

 -(UIFont)getCorrespondingHelveticaNeueFontMatchingCharacteriticsOf:(UIFont *)font { UIFont *helveticaNueue; if (!font) //Checking if there is a font) { helveticaNueue = [UIFont fontWithName@"HelveticaNueue" size:defaultSizeYouSet]; } else { //Check if it BoldItalic/Italic/Bold } return helveticaNueue; } 

checking all the cases that I mentioned earlier.
So, from the previous code, you just need to replace the if tags that work specifically for Calibri with:

 UIFont *newFont = [self getCorrespondingHelveticaNeueFontMatchingCharacteriticsOf:currentFont]; 

Now you can do the third part (and add them to your test): Is the case of a Black font to control a font like Bold ? Well, Helvetica Neue has this “property”, but you can directly use the Black property for Helvetica Neue and you might want to check if it has currentFont (and again another case).

EDIT: You can use with iOS7, we can use UIFontDescriptor . Here is what you could do:

 -(UIFont *)customFontMatchingFont:(UIFont *)otherFont { NSString *customFontName = @"Helvetica"; //The custom font UIFont *tempFont = [UIFont fontWithName:customFontName size:[otherFont pointSize]]; return [UIFont fontWithDescriptor:[[tempFont fontDescriptor] fontDescriptorWithSymbolicTraits:[[otherFont fontDescriptor] symbolicTraits]] size:[otherFont pointSize]]; } 

Note. I did not check all cases (what if your user family does not have the corresponding font, what does it return? Etc.), but this can be a nice addition if you do not do too complicated things.

+12
Apr 18 '14 at 11:30
source share



All Articles