我需要为化学式(H2O、Na^2+等)制作下标?
使用NSAttributedString是否可以实现这一功能,还是有其他/更简单的方法可以制作下标?
我需要为化学式(H2O、Na^2+等)制作下标?
使用NSAttributedString是否可以实现这一功能,还是有其他/更简单的方法可以制作下标?
以下是我在iOS 6中所做的。首先添加CoreText和QuartzCore框架。然后导入:
#import <QuartzCore/QuartzCore.h>
#import <CoreText/CTStringAttributes.h>
#import <CoreText/CoreText.h>
我写了一个小函数,它输入一个普通的NSString并导出一个带有上标的NSMutableAttributedString。可以修改此函数以允许设置上标或下标,将kCTSuperscriptAttributeName值更改为-1即可。此外,您可以添加一个变量来指定字符串中放置上标的位置。现在它只假定在字符串末尾。
- (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;
}
现在当我想要使用它时,我可以按照以下步骤将其放入UITextField中:
NSString *qlwUnitsPlainText = @"m3";
self.quantityLoadWeightUnits_textField.attributedText = [self plainStringToAttributedUnits:qlwUnitsPlainText];
我希望这对其他人有所帮助,因为这方面的示例不是很多!
NSAttributedString
实现。您要查找的属性常量取决于您的平台。对于Mac OS X,它是NSSuperscriptAttributeName
,对于iOS,它是kCTSuperscriptAttributeName
。对于下标,请传递一个负值。UILabel
不能绘制NSAttributedString
(但希望在iOS 6中能够实现)。您需要使用核心文本绘制文本,或查找一些第三方替代UILabel
,该替代品可以绘制NSAttributedString
。UILabel
现在通过attributedString
属性支持NSAttributedString
。kCTSuperscriptAttributeName
的值也可以是介于-1和1之间的小数值,以改变上标/下标文本的位置,以适应您的需求。 - Bensge+ (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;
}
Styles
是什么? - Aadil Keshwani 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];
如果你想让它更整洁一些,可以尝试以下方法
NSDictionary *attr = @{ NSFontAttributeName: smallfont,
(NSString*)kCTSuperscriptAttributeName: @1 }
NSRange fabricWeightRange = NSMakeRange(fabricWeight.location + 2, 1);
[subKeyString setAttributes:attr range:fabricWeightRange];