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
沒有留言:
張貼留言