更改数字小数部分的字体

时间:2014-03-25 16:00:55

标签: ios objective-c cocoa-touch fonts nsnumberformatter

我的问题是,如何更改数字小数部分的字体大小,例如:

Example

到目前为止,我可以分离数字的组成部分,但我无法找到改变小数部分字体大小的解决方案,这是我目前所拥有的:

-(NSString *)formaterDecimale:(CGFloat)valeurReel{
NSNumberFormatter *numberFormatter =[[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setDecimalSeparator:@","];
[numberFormatter setNegativePrefix:@""];
[numberFormatter setGroupingSeparator:@"."];
[numberFormatter setRoundingIncrement:[NSNumber numberWithDouble:0.01]];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];

NSString *formattedString = [numberFormatter stringFromNumber:[NSNumber numberWithFloat:valeurReel]];
return formattedString;

}

最诚挚的问候。

2 个答案:

答案 0 :(得分:1)

您应该创建属性字符串

NSRange range = [formattedString rangeOfString:@"."];
range.length = formattedString.length - range.location;


NSDictionary *normalAttributes = @{ NSFontAttributeName : [UIFont systemFontOfSize:15],
                                   NSForegroundColorAttributeName : [UIColor blackColor] };

NSDictionary *smallAttributes = @{ NSFontAttributeName : [UIFont systemFontOfSize:8],
                                   NSForegroundColorAttributeName : [UIColor grayColor] };

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:formattedString attributes:normalAttributes];
[attributedString setAttributes:smallAttributes range:range];

然后将其分配给UILabel的attributedText属性。

UILabel *your_label; // do not forget to create and initialize it
your_label.attributedText = attributedString;

其他一些控件如UITextView也可以显示属性字符串。

可能这个CGRect for selected UITextRange adjustment for multiline text?答案对你来说也很有意思。

答案 1 :(得分:1)

这样的事情:

-(NSString *)formaterDecimale:(CGFloat)valeurReel{
   NSNumberFormatter *numberFormatter =[[NSNumberFormatter alloc] init];
   [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
   [numberFormatter setDecimalSeparator:@","];
   [numberFormatter setNegativePrefix:@""];
   [numberFormatter setGroupingSeparator:@"."];
   [numberFormatter setRoundingIncrement:[NSNumber numberWithDouble:0.01]];
   [numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];

   NSString *formattedString = [numberFormatter stringFromNumber:[NSNumber numberWithFloat:valeurReel]];

   // Change font of decimal part
   NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:formattedString];
   NSDictionary *decimalAttributes = @{ NSFontAttributeName: [UIFont fontWithName:@"Verdana" size:13] };
   NSRange* decimalRange = [formattedString rangeOfString:@"."];
   [attrString setAttributes:spacingAttributtes range:decimalRange];

   return attrString;
}

UILabel

上使用归因文字