无法为UILabel设置下划线

时间:2014-12-21 03:53:15

标签: ios objective-c iphone ios7 uilabel

我有一个UILabel有文字:“这里的东西和隐私政策”,我只是想为文字隐私政策创建下划线,但代码不会影响。如果我为我的标签的全文创建下划线然后它工作,但它不是我想要的。这是我的代码:

     NSString *str = self.myLabel.text;
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
        NSRange index = [str rangeOfString:@"privacy policy"];
        if (index.location != NSNotFound){
            [attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange(index.location, index.length)];
          //if I change index.location by 0 then it worked, 
          //with the underline start at the begin text of mylabel but that not what I expected
            [self.myLabel setAttributedText:attributedString];
         }

我的代码有什么问题(我对iOS 7和后者的app支持)?

3 个答案:

答案 0 :(得分:0)

要暂时解决您的问题,您可以将String拆分为2个NSMutableAttributedString,后一个字符串包含文本“隐私策略”。然后,您可以为整个文本加下划线并连接两个字符串。

NSString *str = self.myLabel.text;
    NSRange index = [str rangeOfString:@"privacy policy"];
    if (index.location != NSNotFound){
        NSString *tempString = [str substringWithRange:NSMakeRange(0, index.location - 1)];
        NSAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:tempString];

        NSString *privacyString=[str substringFromIndex:index.location];
        NSAttributedString *attributedString1 = [[NSMutableAttributedString alloc] initWithString:privacyString];
        [attributedString1 addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange(0, attributedString1.length)];
      //with the underline start at the begin text of mylabel but that not what I expected
        NSMutableAttributedString *mutableAttString = [[NSMutableAttributedString alloc] init];
        [mutableAttString appendAttributedString: attributedString];
        [mutableAttString appendAttributedString:attributedString1];
        [self.myLabel setAttributedText:mutableAttString];
     }

答案 1 :(得分:0)

请尝试这段代码。

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 250, 40)];
label.font=[UIFont fontWithName:@"Helvetica" size:12];
label.textColor=[UIColor blackColor];

NSString *text = @"Something and Privacy Policy";
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc ]initWithString:text];

NSInteger location = [@"Something and " length]-1;
NSInteger length = [@"Privacy Policy" length];
[attrString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(location, length)];
label.attributedText=attrString;

[self.view addSubview:label];

答案 2 :(得分:0)

我发现强调文本的一部分的最好方法是使用Interface Builder(Designer first,coder second)。当然这可能不是你想要的,但对于临时解决方案,我提出以下建议:

说明
1)在您选择的viewController中放置一个UILabel,并根据需要添加约束 2)单击标签,替换文本并转到属性检查器。 (图1)
3)将您的UILabel从“纯文本”更改为“已归属” 4)突出显示您想要下划线的部分,如下面的图2所示,然后按方形中的“T”。
5)在显示的窗口的工具栏上,您将看到带下划线的“T”。选择“单个”(图3)。

图片

Image 1

Image 2 Image 3