简体   繁体   English

在NSTextView中使指定的文本加粗

[英]Make specified text in NSTextView Bold

I am using a little bit of code to change the text in an NSTextView 我正在使用一些代码来更改NSTextView中的文本

NSString *string = @"This is a title\n\nThis is some info";
[content setString:string];

I would like to make the text "This is a title" the only text that is bold when displaying in the NSTextView and the rest normal text. 我想使文本“ This is a title”成为在NSTextView和其余普通文本中显示时唯一的粗体文本。 Is there any way to do this? 有什么办法吗? Maybe like how the StackOverflow editor works? 也许像StackOverflow编辑器的工作方式一样? __bold text here__

Thanks in advance! 提前致谢!

I'm not sure if you can just apply boldness as that's usually part of another set of glyphs. 我不确定您是否可以仅应用粗体,因为这通常是另一组字形的一部分。 But how's this work... 但是这是怎么回事...

NSString* textUnattributed = @"This is a title\n\nThis is some info";
NSMutableAttributedString* textAttributed = [[NSMutableAttributedString alloc] initWithString:textUnattributed];
[textAttributed addAttribute:NSFontAttributeName
                       value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0]
                       range:[textUnattributed rangeOfString:@"This is a title"]];
content.attributedText = textAttributed;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM