我使用以下代码将字符串的一部分加粗。我只想将Shipment Ref. #:
加粗。以下是我的代码
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];
问题是它没有被加粗
最佳答案
试试这个代码
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];
关于ios - 使用NSMutableAttributedString粗体显示字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38474544/