2013-04-21 42 views
0

我已經在這裏找到這段代碼上,以便幫助我有一個UILabel一個字母間距,這是代碼:子類的UILabel有字母間距不口音信合作

- (void) drawRect:(CGRect)rect 
{ 
// Drawing code 
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSelectFont (context, [self.font.fontName cStringUsingEncoding:NSASCIIStringEncoding], self.font.pointSize, kCGEncodingMacRoman); 
CGContextSetCharacterSpacing(context, 2); 
CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]); 
CGAffineTransform myTextTransform = CGAffineTransformScale(CGAffineTransformIdentity, 1.f, -1.f); 

CGContextSetTextMatrix (context, myTextTransform); 

// draw 1 but invisbly to get the string length. 
CGPoint p =CGContextGetTextPosition(context); 
float centeredY = (self.font.pointSize + (self.frame.size.height- self.font.pointSize)/2)-2; 
CGContextShowTextAtPoint(context, 0, centeredY, [self.text cStringUsingEncoding:NSASCIIStringEncoding], [self.text length]); 
CGPoint v =CGContextGetTextPosition(context); 

// calculate width and draw second one. 
float width = v.x - p.x; 
float destX = 0; 

// calculate X position based on alignment 
if([self textAlignment] == UITextAlignmentLeft){ 
    destX = 0; 
}else if([self textAlignment] == UITextAlignmentCenter){ 
    destX = (self.frame.size.width- width)/2; 
}else if([self textAlignment] == UITextAlignmentRight){ 
    destX = self.frame.size.width - width; 
} 
CGContextSetFillColorWithColor(context, [self.textColor CGColor]); 
CGContextShowTextAtPoint(context, destX, centeredY, [self.text cStringUsingEncoding:NSASCIIStringEncoding], [self.text length]); 
} 

問題是那樣的口音字母像èàòéuilabel是空的,我怎麼能解決這個問題?

回答

0

加載字體時可能使用了錯誤的編碼。嘗試改變這一行:

CGContextSelectFont (context, [self.font.fontName cStringUsingEncoding:NSASCIIStringEncoding], self.font.pointSize, kCGEncodingMacRoman); 

使用此編碼改爲:

CGContextSelectFont (context, [self.font.fontName cStringUsingEncoding:NSASCIIStringEncoding], self.font.pointSize, kCGEncodingFontSpecific); 

然後,你輸出你正在使用這種編碼NSASCIIStringEncoding的字符串,根據蘋果文檔最肯定不包括字符你要。所以即使你使用正確的編碼閱讀字符集,你也不會輸出它。

NSASCIIStringEncoding 
Strict 7-bit ASCII encoding within 8-bit chars; ASCII values 0…127 only. 
Available in iOS 2.0 and later. 
+0

我都試過了,現在我的所有符號,而不是信...不工作... – Piero 2013-04-21 16:35:36

+0

那好吧好:蘋果文檔的編碼您使用輸出字符串

因爲找出你的加載和使用字體的正確編碼是什麼。但它很可能是編碼不匹配 – 2013-04-21 16:37:24

+0

我解決了這種編碼:kCGEncodingMacRoman – Piero 2013-04-21 19:22:34