标签UnderLine不显示

时间:2013-12-19 09:16:08

标签: ios uilabel core-text

我想在标签上添加下划线。为此,我使用了以下代码。我正在使用自定义字体:

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];

2 个答案:

答案 0 :(得分:1)

使用以下代码。这对我有用。

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

如果不起作用,则问题可能出在您的自定义字体

答案 1 :(得分:0)

NSString *tempString=@"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)];