1

Im using the following code to bold a part of string. I want only the Shipment Ref. #: to be bolded. Following is my code

  UIFont *boldFont = [UIFont boldSystemFontOfSize:12];
    NSString *yourString = [NSString stringWithFormat:@"Shipment Ref. #: %@ ",[[[NSUserDefaults alloc] init] valueForKey:@"shipRef"]];
    NSRange boldedRange = NSMakeRange(10, 4);

    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:yourString];

    [attrString beginEditing];
    [attrString addAttribute:NSFontAttributeName
                       value:boldFont
                       range:boldedRange];

    [attrString endEditing];
    self.shipRefHeader.text = [attrString mutableString];

The problem is that it is not getting bolded

1
  • 2
    self.shipRefHeader.text = [attrString mutableString]; => self.shipRefHeader.attributedText = attrString; Commented Jul 20, 2016 at 7:10

1 Answer 1

3

try this code

    NSString * yourString = [NSString stringWithFormat:@"Shipment Ref. #: %@ ",[[[NSUserDefaults alloc] init] valueForKey:@"shipRef"]];;
    NSMutableAttributedString * attrString = [[NSMutableAttributedString alloc] initWithString:yourString];
    NSString *boldString = @"Shipment Ref. #:";
    NSRange boldRange = [yourString rangeOfString:boldString];
    [attrString addAttribute: NSFontAttributeName value:[UIFont boldSystemFontOfSize:12] range:boldRange];
    [self.shipRefHeader setAttributedText: attrString];
Sign up to request clarification or add additional context in comments.

1 Comment

I didn't check the whole code of the author, but if I'm write, it's only error was that he/she was setting the text property instead of the attributedText one. Your answer doesn't point at all that mistake, you don't give any justification or explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.