2013-12-19 141 views
0

我想爲標籤添加下劃線。爲此,我使用了下面的代碼。我正在使用自定義字體:標籤在線不顯示

headingLbl.font=[UIFont fontWithName:GZFont size:20.0f]; 
NSMutableAttributedString *gpsSearch = [[NSMutableAttributedString alloc] initWithString:@"GPS Search"]; 
[gpsSearch addAttribute:(NSString*)kCTUnderlineStyleAttributeName 
         value:[NSNumber numberWithInt:kCTUnderlineStyleSingle] 
         range:(NSRange){0,[gpsSearch length]}]; 
self.headingLbl.attributedText = gpsSearch; 
self.headingLbl.textColor = [UIColor blackColor]; 
+0

這是一個多行'UILabel'?因爲如果不是,那麼你可以覆蓋'UILabel'類的'drawRect' - 鏈接:http://stackoverflow.com/questions/2711297/underline-text-in-uilabel – staticVoidMan

回答

1

使用下面的代碼。這對我有效。

NSMutableAttributedString *gpsSearch = [[NSMutableAttributedString alloc] initWithString:@"GPS Search"]; 
[gpsSearch addAttribute:NSUnderlineStyleAttributeName 
         value:[NSNumber numberWithInt:NSUnderlineStyleSingle] 
         range:(NSRange){0,[gpsSearch length]}]; 

如果沒有奏效,那麼問題可能是您的自定義字體

0
NSString *[email protected]"your string"; 

NSMutableAttributedString *attString=[[NSMutableAttributedString  
alloc]initWithString:tempString];  

int length=(int)[tempString length]; 

[attString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:2] 
range:NSMakeRange(0,length-1)];