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

2015年10月3日 星期六

iOS 開發筆記 - UITableViewCell 在 iOS9 的環境中,預設沒有佔滿 UITableView Width

假設 UITableView 的寬度有 768,但 UITableViewCell 拿不到 768 ,導致用程式計算的排版會出錯,且連 titleForHeaderInSection 顯示也一樣,追了一下應該是 Margin 效果

https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UITableView_Class/#//apple_ref/occ/instp/UITableView/cellLayoutMarginsFollowReadableWidth

偷懶解法:

if([self.tableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)])
self.tableView.cellLayoutMarginsFollowReadableWidth = NO;

2014年7月6日 星期日

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