顯示具有 font 標籤的文章。 顯示所有文章
顯示具有 font 標籤的文章。 顯示所有文章

2016年2月20日 星期六

修正 Windows VLC UTF-8 字幕亂碼的問題 @ Windows 10

首先,字幕很確定是 UTF-8 了,因為在 VLC @ Mac OS X 是正常的,但一樣的檔案搬去 Windows 10 則無法正常顯示。

VLC Player -> 工具 -> 偏好設定:

  • 預設編碼: UTF-8
  • 字型:Microsoft YaHei
一開始以為換編碼就行了,接著以為字型換中文(細明體)的就行了,最後還是上網查別人的招數,竟然連字型也是個關鍵 Orz

2014年7月6日 星期日

iOS 開發筆記 - 列出系統內建 monospaced font (等寬字型/等寬字體)

列出系統所有字型:

for (int i=0 ; i< [[UIFont familyNames] count]; ++i) {
        NSLog(@"Font Family: %@", [UIFont familyNames][i]);
        for (int j=0; j<[[UIFont fontNamesForFamilyName:[UIFont familyNames][i]] count] ; ++j) {
            NSLog(@"Font Name: %@", [UIFont fontNamesForFamilyName:[UIFont familyNames][i]][j]);
        }
}


找尋 monospaced font:

NSString *words = @"Hello World!";
for (int i=0 ; i< [[UIFont familyNames] count]; ++i) {
    //NSLog(@"Font Family: %@", [UIFont familyNames][i]);
    for (int j=0; j<[[UIFont fontNamesForFamilyName:[UIFont familyNames][i]] count] ; ++j) {
        //NSLog(@"Font Name: %@", [UIFont fontNamesForFamilyName:[UIFont familyNames][i]][j]);
     
        float width = -1 ;
        bool found = true;
        for (int k=0; k< [words length]; ++k) {
            unichar word = [words characterAtIndex:k];
            CGSize wordSize = [[NSString stringWithFormat:@"%C", word] sizeWithAttributes:@{
                NSFontAttributeName: [UIFont fontWithName:[UIFont fontNamesForFamilyName:[UIFont familyNames][i]][j] size:16]
            }];
            if (width < 0) {
                width = wordSize.width;
            } else if (width != wordSize.width) {
                found = false;
                break;
            }
        }
        if (found) {
            NSLog(@"width(size16): %f, monospaced font: %@", width, [UIFont fontNamesForFamilyName:[UIFont familyNames][i]][j]);
        }
    }
}


結果:

width(size16): 9.601562, monospaced font: CourierNewPS-BoldMT
width(size16): 9.601562, monospaced font: CourierNewPS-ItalicMT
width(size16): 9.601562, monospaced font: CourierNewPSMT
width(size16): 9.601562, monospaced font: CourierNewPS-BoldItalicMT
width(size16): 9.601562, monospaced font: Courier-BoldOblique
width(size16): 9.601562, monospaced font: Courier
width(size16): 9.601562, monospaced font: Courier-Bold
width(size16): 9.601562, monospaced font: Courier-Oblique
width(size16): 9.632812, monospaced font: Menlo-Italic
width(size16): 9.632812, monospaced font: Menlo-Bold
width(size16): 9.632812, monospaced font: Menlo-Regular
width(size16): 9.632812, monospaced font: Menlo-BoldItalic

iOS 開發筆記 - 計算一個字的寬度

程式碼:     NSString *words = @"Hello World! 您好!";
    for( int i=0 ; i<[words length] ; ++i ) {
        unichar word = [words characterAtIndex:i];
        CGSize wordSize = [[NSString stringWithFormat:@"%C", word] sizeWithAttributes:nil];
        NSLog(@"Char: %C, Font Width: %f", word, wordSize.width);
    }


Char: H, Font Width: 8.666016
Char: e, Font Width: 6.673828
Char: l, Font Width: 2.666016
Char: l, Font Width: 2.666016
Char: o, Font Width: 6.673828
Char:  , Font Width: 3.333984
Char: W, Font Width: 11.326172
Char: o, Font Width: 6.673828
Char: r, Font Width: 3.996094
Char: l, Font Width: 2.666016
Char: d, Font Width: 6.673828
Char: !, Font Width: 3.333984
Char:  , Font Width: 3.333984
Char: 您, Font Width: 12.000000
Char: 好, Font Width: 12.000000
Char: !, Font Width: 12.000000


若要指定 font ,可以透過設定 Attributes:

@{
    NSFontAttributeName: [UIFont fontWithName:@"Courier" size:16]
}


指定 Courier 字型: Char: H, Font Width: 9.601562
Char: e, Font Width: 9.601562
Char: l, Font Width: 9.601562
Char: l, Font Width: 9.601562
Char: o, Font Width: 9.601562
Char: , Font Width: 9.601562
Char: W, Font Width: 9.601562
Char: o, Font Width: 9.601562
Char: r, Font Width: 9.601562
Char: l, Font Width: 9.601562
Char: d, Font Width: 9.601562
Char: !, Font Width: 9.601562
Char: , Font Width: 9.601562
Char: 您, Font Width: 16.000000
Char: 好, Font Width: 16.000000
Char: !, Font Width: 16.000000

2014年7月1日 星期二

iOS 開發筆記 - 使用自定字型



用法:
  1. 將字形(MyCustomFontName.ttf)拖進Supporting Files,記得要勾選 "Add to tagets"

  2. 更新 *.plist 資訊,新增 Fonts provided by application,新增剛剛添加的字型檔名(含副檔名)
  3. 簡易使用 NSLog(@"MyCustomFont: %@", [UIFont fontWithName:@"MyCustomFontName" size:12]);  ,不需副檔名,若 NSLog 輸出不是 null 即可。

2013年12月11日 星期三

iOS 開發筆記 - 使用 Core Graphics 進行文字繪圖輸出並設定 NSString Attributes (Font、Alignment) 等

draw string

整理一年多前的程式碼,發現用的函數已經在 iOS 7 宣告為 deprecated 啦,恰好重看 CS193P 時,得知 NSAttributedString 這東西,以為是最新出來的,沒想到這在 iOS 3.2 就存在啦。

順便把一些筆記都記下吧:

+ (UIImage *)buildImageAndDrawText:(NSString *)text size:(CGSize)size ios7:(BOOL)iOS7SDK
{
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, 1, 1, 1, 1);
    CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height));

    UIFont *font = [UIFont italicSystemFontOfSize:15];
    UIColor *fontColor = [UIColor blueColor];

    if (iOS7SDK) {
        NSMutableParagraphStyle *paragrapStyle = [[NSMutableParagraphStyle alloc] init];
        [paragrapStyle setLineBreakMode:NSLineBreakByWordWrapping];
        [paragrapStyle setAlignment:NSTextAlignmentCenter];
     
        [text drawInRect:CGRectIntegral(CGRectMake(0, size.height/2, size.width, size.height))
          withAttributes:@{
                           NSParagraphStyleAttributeName: paragrapStyle,
                           NSFontAttributeName: font,
                           NSForegroundColorAttributeName: fontColor}
         ];
    } else {
        [fontColor set];
        [text drawInRect:CGRectIntegral(CGRectMake(0, size.height/2, size.width, size.height)) withFont:font lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentCenter];
    }
 
    UIImage *outImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return outImage;
}


用法:

self.imageView = [YourClass buildImageAndDrawText:@"iOS app development @ blog.changyy.org" size:CGSizeMake(300, 300) ios7:YES];

細節可參考 Core Graphics Framework Reference