2017-02-28 44 views
0

當我使用boundingRectWithSize來計算文本的高度時,它會崩潰。我發現它可能是因爲文本中有一些表情符號。有人可以告訴我如何解決這個問題。boundingRectWithSize與表情符號崩潰

CGFloat kScreenMargin = 15.; 
    CGFloat kScreenWidth = [UIScreen mainScreen].bounds.size.width; 

    self.content = @"Eefggg‍‍"; 

    CGFloat contentH = [self.content boundingRectWithSize:CGSizeMake(kScreenWidth - 2 * kScreenMargin, MAXFLOAT) 
                options:NSStringDrawingUsesLineFragmentOrigin 
               attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size.height; 

Here is the crash screenshot

回答

0

你先轉換成編碼字符串,然後得到高度:

CGFloat kScreenMargin = 15.; 
    CGFloat kScreenWidth = [UIScreen mainScreen].bounds.size.width; 
NSString *strEmo = @"Eefggg‍‍" 
NSData *data = [strEmo dataUsingEncoding:NSNonLossyASCIIStringEncoding]; 
NSString *goodValue = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
self.content = goodValue; 
CGFloat contentH = [self.content boundingRectWithSize:CGSizeMake(kScreenWidth - 2 * kScreenMargin, MAXFLOAT) 
                options:NSStringDrawingUsesLineFragmentOrigin 
               attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size.height; 
+0

如何做好中國 – iCloudy

+0

@iCloudy http://stackoverflow.com/questions/14619316/chiness -charecter-編碼問題,iphone – 2017-02-28 06:32:37