我是iOS Development的新手。我無法讓我的字符串變爲BOLD。試圖將動態NSString文本轉換爲粗體文本
在此先感謝! 這裏是我的代碼
- (void)viewDidLoad {
[super viewDidLoad];
NSString *sentence = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%@%@%@%@", self.name, self.adjective, self.noun, self.verb]];
NSMutableAttributedString *finalSentence = [[NSMutableAttributedString alloc] initWithString:sentence];
NSRange nameRange = NSMakeRange(0, self.name.length);
NSRange adjectiveRange = NSMakeRange(self.name.length, self.adjective.length);
NSRange nounRange = NSMakeRange((self.adjective.length + self.name.length), self.noun.length);
NSRange verbRange = NSMakeRange((self.adjective.length + self.name.length + self.noun.length), self.verb.length);
[finalSentence beginEditing];
[finalSentence addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldMT" size:17.0] range:nameRange];
[finalSentence addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldMT" size:17.0] range:adjectiveRange];
[finalSentence addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:12.0] range:nounRange];
[finalSentence addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:12.0] range:verbRange];
[finalSentence endEditing];
NSString *completeSentence = [finalSentence string];
self.resultTextView.text = completeSentence;
}