2013-12-19 114 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)];