当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - Bolding a string using NSMutableAttributedString

问题描述:

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

网友答案:

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];
分享给朋友:
您可能感兴趣的文章:
随机阅读: