2

I have a UILabel which have text: "Something here and privacy policy", I just want to create underline for the text privacy policy but the code not affect. If I create underline for full text of my label then it worked, but it's not what I want.Here is my code:

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

Is have something wrong with my code(my app support for iOS 7 and latter)?

lee
  • 7,955
  • 8
  • 44
  • 60

3 Answers3

0

To temporarily fix your issue, you can split your String into 2 NSMutableAttributedString with latter string having text "privacy policy". You can then underline the whole text and concatenate the two strings.

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];
     }
Arun Gupta
  • 2,628
  • 1
  • 20
  • 37
0

Please try this piece of code.

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];
Ruchira Randana
  • 4,021
  • 1
  • 27
  • 24
0

The best way I've found to underline a portion of a text is to use Interface Builder (Designer first, coder second). Granted this might not be what you want but for a temporary solution I propose the following:

Instructions
1) Place a UILabel in your viewController of choice and add constraints as needed.
2) Click on your label, replace the text and go to Attributes Inspector. (Image 1)
3) Change your UILabel from 'Plain Text' to 'Attributed'
4) Highlight the portion you want underlined as shown in Image 2 below and press the 'T' in a square.
5) On the toolbar of the window that shows up, you'll see an underlined 'T'. Select 'single' (Image 3).

Images

Image 1

Image 2 Image 3

cyril
  • 3,020
  • 6
  • 36
  • 61