iOS 字符串

来源:互联网 发布:ubuntu如何安装输入法 编辑:程序博客网 时间:2024/06/05 12:04

- (NSString *)RMBStringWithPriceString:(NSString *)price {

    NSNumberFormatter  *formatter = [[NSNumberFormatteralloc] init];

    NSNumber *RMBNumber = [formatter numberFromString:price];

    formatter.numberStyle =NSNumberFormatterCurrencyStyle;

    NSString *rmb = [formatter stringFromNumber:RMBNumber];

    //$替换成¥

    if ([rmb hasPrefix:@"$"]) {

        rmb = [rmb stringByReplacingOccurrencesOfString:@"$"withString:@""];

    }

    return rmb;

}

其中NSNumberFormatter类有个属性numberStyle,它是一个枚举型,设置不同的值可以输出不同的数字格式。该枚举包括: 
enum { 
    NSNumberFormatterNoStyle = kCFNumberFormatterNoStyle, 
    NSNumberFormatterDecimalStyle = kCFNumberFormatterDecimalStyle, 
    NSNumberFormatterCurrencyStyle = kCFNumberFormatterCurrencyStyle, 
    NSNumberFormatterPercentStyle = kCFNumberFormatterPercentStyle, 
    NSNumberFormatterScientificStyle = kCFNumberFormatterScientificStyle, 
    NSNumberFormatterSpellOutStyle = kCFNumberFormatterSpellOutStyle 
}; 
typedef NSUInteger NSNumberFormatterStyle; 
各个枚举对应输出数字格式的效果如下: 
[1243:403] Formatted number string:123456789 
[1243:403] Formatted number string:123,456,789 
[1243:403] Formatted number string:¥123,456,789.00 
[1243:403] Formatted number string:-539,222,988% 
[1243:403] Formatted number string:1.23456789E8 
[1243:403] Formatted number string:一亿二千三百四十五万六千七百八十九 

其中第三项和最后一项的输出会根据系统设置的语言区域的不同而不同。



  NSMutableAttributedString *attString  = [[NSMutableAttributedStringalloc]initWithString:@"0.00元起"];

     [attString addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:30.f]     range:NSMakeRange(0, textString.length -2)];

    [attString addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:14.f]     range:NSMakeRange(textString.length -2,2)];



NSMutableAttributedString * attrString = [[NSMutableAttributedStringalloc] initWithString:tempString];

    [attrString addAttribute:NSForegroundColorAttributeNamevalue:[UIColorwhiteColor] range:NSMakeRange(0, tempString.length)];

    [attrString addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:10.f]     range:NSMakeRange(0, tempString.length)];

    [attrString addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:6.0f]     range:NSMakeRange(tempString.length -1, 1)];

    [attrString addAttribute:NSBaselineOffsetAttributeNamevalue:@(5) range:NSMakeRange(tempString.length -1, 1)];



// 设置小下标

- (NSAttributedString *)makeNO2smallCount:(NSString *)str

{

    NSMutableAttributedString * attrStr = [[NSMutableAttributedStringalloc] initWithString:str];

    NSInteger len = str.length;

    [attrStr addAttribute:NSBaselineOffsetAttributeNamevalue:@(-1)range:NSMakeRange(len-1,1)];

    [attrStr addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:7.0f]range:NSMakeRange(len-1,1)];

    return attrStr;


}



0 0
原创粉丝点击