NSTextView使用数组做大胆的文字 [英] NSTextView making bold text using an array

查看:349
本文介绍了NSTextView使用数组做大胆的文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有具有以下结构的阵列:

I have an array which has the following structure:

(
    [0] = (
         [0] = @"Title string"
         [1] = @"Some content string"
    )
    [1] = (
         [0] = @"Title string"
         [1] = @"Some content string"
    )
    [2] = (
         [0] = @"Title string"
         [1] = @"Some content string"
    )
    ...
)

和等等等等第四至复发的variating量。

and so on and so fourth to a variating amount of reoccurrence.

我的目标是尝试,这一切合并成一个单一的字符串中的NSTextField显示,并且使每一个标题字符串大胆。因此,code以上会是这个样子,如果它被输出。

My goal is to try and merge it all into one single string to display in an NSTextField, and make every title string bold. So the code above would look something like this if it were outputted.



标题字符串结果
有些内容字符串


Title String
Some content string

标题字符串结果
有些内容字符串

Title String
Some content string

标题字符串结果
有些内容字符串

Title String
Some content string

我的第一个问题是如何使一个单一的字符串,其中某些粗体文字;我的第二个问题是我怎么能采取的字符串,并将其发送到的NSTextField?

My first question is how could make a single string where certain text is bold; and my second question is how could I take that string and send it to the NSTextField?

到目前为止,这是我做了什么结果,我试过用 NSMutableAttributedString ,使像这样的字符串:

So far this is what I've done.
I've tried using NSMutableAttributedString to make the string like so:

NSMutableAttributedString *contentString = [NSMutableAttributedString alloc];

for (int i=0; i<[result count]; i++) {

    [contentString addAttribute:NSFontAttributeName
                value:[NSFont fontWithName:@"HelveticaNeue-Bold" size:16.0]
                range:NSRangeFromString(result[i][0])];
}

但我无法弄清楚如何做到这独自追加其他任何字符串,或尝试和打印,因为我想下面的的NSTextField显示它时,这个错误。

But I couldn't figure out how to append anything else to the string, or try and print just that alone because I got this error when trying to the following to display it in the NSTextField.

[self->contentView setString:contentString];

不兼容的指针类型发送NSMutableAttributedString *来型paramater'的NSString *'

所以,我想这个代替,却得到了不同的错误

So I tried this instead, but got a different error

[self->contentView attributedString:contentString];

为NSTextView无可见@interface声明选择attributedString

推荐答案

设法找到一种方法,使工作

Managed to find a way to make it work

NSMutableAttributedString *contentString = [NSMutableAttributedString alloc];

for (int i=0; i<[result count]; i++) {    
    NSMutableAttributedString *resultString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n%@\n\n", result[i][0], result[i][1]]];
    NSDictionary *attributes = @{
        NSFontAttributeName : [NSFont fontWithName:@"HelveticaNeue-Bold" size:12.0]
    };
    NSString *subtitle = [NSString stringWithFormat:@"%@", result[i][0]];

    [resultString setAttributes:attributes range:NSMakeRange(0, [subtitle length])];
    [contentString appendAttributedString:resultString];
}

[self->content setString:@""];
[[self->content textStorage] appendAttributedString:contentString];

解决之道在于code的最后一行中。所有这一切需要做了,而不是将数据传递到了setString ,使用 textStorage 代替,并传递一个对象给它。 (我觉得我得到的术语右)

The solution lies within the last line of code. All that needed to be done was instead of passing data to setString, use textStorage instead and pass an object to it. (I think I got the terminology right)

武卫耶希望这可以帮助任何人在未来!

Bue yeah hope this helps anyone in the future!

这篇关于NSTextView使用数组做大胆的文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆