Jak tworzyć indeksy dolne i górne za pomocą NSAttributedString?
Muszę zrobić indeksy dla formuł chemicznych (H2O, na^2+, itp)?
Czy jest to możliwe z NSAttributedString, czy istnieje alternatywny / łatwiejszy sposób tworzenia indeksów dolnych?
5 answers
Jest to możliwe z NSAttributedString
. Stała atrybutu, której szukasz, zależy od twojej platformy. Dla Mac OS X jest to NSSuperscriptAttributeName
, A Dla iOS jest to kCTSuperscriptAttributeName
. Podaj wartość ujemną dla indeksu dolnego.
Jedynym zastrzeżeniem jest to, że UILabel
na iOS nie można rysować NSAttributedString
s (jeszcze trzymam kciuki za iOS 6). Trzeba by narysować tekst za pomocą Core Text lub znaleźć jakiś zamiennik dla UILabel
, który może narysować NSAttributedString
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-18 23:46:27
Oto co zrobiłem w iOS 6. Najpierw dodaj frameworki CoreText i QuartzCore. Następnie import:
#import <QuartzCore/QuartzCore.h>
#import <CoreText/CTStringAttributes.h>
#import <CoreText/CoreText.h>
Zrobiłem małą funkcję, która wprowadza zwykły NSString i eksportuje NSMutableAttributedString z ostatnim znakiem w indeksie górnym. Można to zmienić, aby umożliwić ustawienie indeksu górnego lub dolnego, zmienić wartość kCTSuperscriptAttributeName na -1. Możesz również dodać zmienną, aby określić, gdzie umieścić indeks górny w łańcuchu. W tej chwili zakłada się tylko koniec sznurek.
- (NSMutableAttributedString *)plainStringToAttributedUnits:(NSString *)string;
{
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:string];
UIFont *font = [UIFont systemFontOfSize:10.0f];
UIFont *smallFont = [UIFont systemFontOfSize:9.0f];
[attString beginEditing];
[attString addAttribute:NSFontAttributeName value:(font) range:NSMakeRange(0, string.length - 2)];
[attString addAttribute:NSFontAttributeName value:(smallFont) range:NSMakeRange(string.length - 1, 1)];
[attString addAttribute:(NSString*)kCTSuperscriptAttributeName value:@"1" range:NSMakeRange(string.length - 1, 1)];
[attString addAttribute:(NSString*)kCTForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, string.length - 1)];
[attString endEditing];
return attString;
}
Teraz, gdy chcę go użyć, mogę wykonać następujące czynności, aby umieścić go w UITextField:
NSString *qlwUnitsPlainText = @"m3";
self.quantityLoadWeightUnits_textField.attributedText = [self plainStringToAttributedUnits:qlwUnitsPlainText];
Mam nadzieję, że to pomoże komuś innemu, nie ma zbyt wielu przykładów!
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-06 17:21:26
Na iOS przegapiłem stałą kCTSuperscriptAttributeName, ale miałem dobre wyniki z rozmiarem czcionki i "baseline". Daje to nieco większą kontrolę również dla mniej posłusznych czcionek:
+ (NSAttributedString *)attributedStringForText:(NSString *)normalText andSuperscript:(NSString *)superscriptText textSize:(CGFloat)textSize
{
UIFont *normalFont = [Styles mainFontWithSize:textSize];
UIFont *superFont = [Styles mainFontWithSize:textSize / 2];
NSMutableAttributedString *finalStr = [[NSMutableAttributedString alloc] initWithString:normalText attributes:@{NSFontAttributeName: normalFont}];
NSAttributedString *superStr = [[NSAttributedString alloc] initWithString:superscriptText attributes:@{NSFontAttributeName: superFont, NSBaselineOffsetAttributeName:@(textSize/2)}];
[finalStr appendAttributedString:superStr];
return finalStr;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-06 08:31:25
Możesz również wykonać następujące czynności, jeśli chcesz, aby był to środek czyszczący litle
NSDictionary *attr = @{ NSFontAttributeName: smallfont,
(NSString*)kCTSuperscriptAttributeName: @1 }
NSRange fabricWeightRange = NSMakeRange(fabricWeight.location + 2, 1);
[subKeyString setAttributes:attr range:fabricWeightRange];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-06 08:26:33
Dla indeksu dolnego użyj wartości dla kCTSuperscriptAttributeName jako @-1.
Zgodnie z dokumentem
@ discussion Value must be a CFNumberRef. Wartość domyślna to int wartość 0. Jeśli obsługiwane przez podaną czcionkę, wartość 1 umożliwia znakowanie i wartość -1 umożliwia zapisywanie.
Extern const CFStringRef kCTSuperscriptAttributeName CT_AVAILABLE (10_5, 3_2);
Example- [lblHeader setText:@“Headers [Alpha1 – text”];
NSMutableAttributedString *headerSubscript = [[NSMutableAttributedString alloc]initWithAttributedString: lblHeader.attributedText];
[headerSubscript addAttribute:(NSString *)kCTSuperscriptAttributeName value:@-1 range:NSMakeRange(14,1)];
[lblHeader setAttributedText:headerSubscript];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-27 06:30:04