Creepify text - 组合字符

时间:2014-05-09 03:35:49

标签: ios unicode nsstring

我想用js代码在我的应用程序中创建creepify文本(creepify()) https://github.com/combatwombat/Lunicode.js/blob/master/lunicode.js

使用此js代码的网站:http://lunicode.com/creepify

我的问题是当我尝试组合字符时,结果就像在网站上一样。

我尝试创建3个NSMutableArray

NSMutableArray *diacriticsTop;
NSMutableArray *diacriticsMiddle;
NSMutableArray *diacriticsBottom;

并像这样存储价值

for (int i = 768; i <= 789; i++) {
    [diacriticsTop addObject:[NSNumber numberWithInt:i]];
}

for (int i = 790; i <= 819; i++) {
if (i != 794 && i != 795) {
    [diacriticsBottom addObject:[NSNumber numberWithInt:i]];
    }
}
...

然后我将js代码中的creepify()转换为

- (NSString *)creepifyString:(NSString *)inputString
{
    BOOL isTop = YES;
    BOOL isMiddle = YES;
    BOOL isBottom = YES;
    NSInteger maxHeight = 15;
    NSInteger randomization = 100;

    NSMutableString *outputString = [[NSMutableString alloc] init];
    unichar output = 0;

    for (int i = 0; i < inputString.length; i++) {
    unichar c = [inputString characterAtIndex:i];
    if (isMiddle) {
        NSNumber *temp = diacriticsMiddle[lroundf(drand48()*diacriticsMiddle.count)];
        c = c + (unichar) temp;
    }
    // Top
    if (isTop) {

        // Put up to this.options.maxHeight random diacritics on top.
        // optionally fluctuate the number via the randomization value (0-100%)
        // randomization 100%: 0 to maxHeight
        //                30%: 70% of maxHeight to maxHeight
        //                 x%: 100-x% of maxHeight to maxHeight
    int diacriticsTopLength = diacriticsTop.count - 1;
    for (int count = 0,
             len = maxHeight - drand48()*((randomization/100)*maxHeight); count < len; count++) {
            NSNumber *temp = diacriticsTop[lroundf(drand48()*diacriticsTopLength)];
            c = c + (unichar) temp;
        }
    }


    // Bottom
    if (isBottom) {
        int diacriticsBottomLength = diacriticsBottom.count - 1;
        for (int count = 0,
             len = maxHeight - drand48()*((randomization/100)*maxHeight); count < len; count++) {

            NSNumber *temp =diacriticsBottom[lroundf(drand48()*diacriticsBottomLength)];
            c = c + (unichar) temp;
        }
    }

    output = output + c;
}

[outputString appendFormat:@"%C", output];

NSLog(@"%@", outputString);

return outputString;

}

0 个答案:

没有答案