How to create an index using NSAttributedString

I see that for NSAttributedString there is a special attribute for a superscript, but I can not find it for the index. What is the general practice of using NSAttributedString to create an index character?

Example: H2O

+6
cocoa nsattributedstring
source share
2 answers

Try NSSuperscriptAttributeName with a negative value.

Otherwise, the hard way would be to replace the characters [0123456789] with [β‚€β‚β‚‚β‚ƒβ‚„β‚…β‚†β‚‡β‚ˆβ‚‰] in the string.

+5
source share

I struggled with indexes / superscripts for some time in NSMutableAttributedString, because for most basic solutions, you need to enter NSRange for every character you want to index. Surely, is there a more automatic way to do things?

OK, yes, but it does take a bit of work.

My method is to indicate characters, indexed, superscript, italics, etc. in NSString, by including the text to be changed using the% signs, followed by information about what type of font adjustment should be done, for example. "The force on the second particle is given by the f-index-b", in my diagram it will be written @ "The force on the second particle is determined by the expression f% & sb%".

Then I use a convenient method:

 NSArray *substrings = [string componentsSeparatedByString:@"%"]; 

to split a string into substrings delimited by% signs, for example.

@ "Hello% & Bhow is% you?" β†’ an array containing the elements: @ "Hello", @ "& Bhow are", @ "you?"

Then I check the first character of each element of the array to see if it contains the character a and mark, which I use to indicate the following character: B = Bold, S = Superscript, I = italics, etc.

So, in the above example, the substring @ "& Bhow are" is intended to be converted to the bold string " like ", and the input @ "Hello% & Bhow are you? Is intended to be converted to" Hello how are you? ".

All font modifications are performed using NSMutableAttributedString and its associated methods, and finally, all NSMutableAttributedString substrings can be inserted back together using methods such as 'appendAttributedString'.

If anyone is interested, my code is as follows:

 -(void) appendFontString:(NSMutableAttributedString*) attribString :(NSString*) string{ NSArray *substrings = [string componentsSeparatedByString:@"%"]; for(int i=0;i<(int) [substrings count];i++){ if([substrings[i] length]>0){ NSString* firstCharacter=[substrings[i] substringToIndex:1]; if([firstCharacter isEqualToString:@"&"]){ NSString* fontType=[substrings[i] substringWithRange:NSMakeRange(1, 1)]; //remove first two characters NSString* newSubString=[substrings[i] substringFromIndex:2]; if([fontType isEqualToString:@"S"]){ [self appendWithSuperscript:attribString :newSubString]; } else if([fontType isEqualToString:@"s"]){ [self appendWithSubscript:attribString :newSubString]; } else if([fontType isEqualToString:@"B"]){ [self appendWithBold:attribString :newSubString]; } else if([fontType isEqualToString:@"I"]){ [self appendWithItalics:attribString :newSubString]; } } else{ //regular string [self append:attribString :substrings[i]]; } } } } 

where are the appendWithBold methods, etc. are user-created methods that convert NSString to formatted bold / superscript / subscript / etc. NSMutableAttributedString, and then add it to the 'attribString' variable.

Mina may not be the best method, but at least it demonstrates that with a little work you can automate subtyping and add-ins in Cocoa.

+2
source share

All Articles