【问题标题】:How to properly set underline type under Cocoa如何在 Cocoa 下正确设置下划线类型
【发布时间】:2019-06-26 04:00:43
【问题描述】:

所有,

NSMutableDictionary* constattrs = [NSMutableDictionary dictionaryWithCapacity:5];
[attrs setObject:[NSNumber numberWithInt:( NSUnderlinePatternDot )] forKey:NSUnderlineStyleAttributeName];
[attrs setValue:style.GetUnderlineColour() forKey:NSUnderlineColorAttributeName];
[m_textView setTypingAttributes:attrs];

上面的代码可以正常编译并执行,但是视图中的文本没有变成下划线。

我正在 OSX 10.13 下进行测试,最低要求为 10.9。

我错过了什么?

TIA!

[编辑]:

    NSRange range = NSMakeRange(start, end-start);

    NSTextStorage* storage = [m_textView textStorage];
    if( style.HasFontUnderlined() )
    {
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        [dict setObject:[NSNumber numberWithInt:( NSUnderlinePatternDot )] forKey:NSUnderlineStyleAttributeName];
        [dict setValue:style.GetUnderlineColour() forKey:NSUnderlineColorAttributeName];

        [storage addAtributes:dict range:range];
        [dict release];

我在上面的代码中收到编译警告:

warning: instance method '-addAtributes:range:' not found (return type defaults to 'id'); did you mean '-addAttributes:range:'? [-Wobjc-method-access]

你能帮忙吗?

[/编辑]

【问题讨论】:

  • 您想给现有文本加下划线吗? NSUnderlineStyleSingle 有效吗?
  • @Willeke,不 - 将写入视图的文本
  • 如何将文本写入视图?
  • @Willeke,我正在调用 `[m_textView insertText:wxCFStringRef ]。

标签: macos styles nstextview


【解决方案1】:

您缺少下划线样式。

[attrs setObject:[NSNumber numberWithInt:( NSUnderlineStyleSingle | NSUnderlinePatternDot )] forKey:NSUnderlineStyleAttributeName];

【讨论】:

  • 这行得通 - 谢谢。你能不能看看我的编辑,看看我错过了什么? TIA!
  • @NV,我想我的眼睛不再那么明亮了。我接受了答案。
猜你喜欢
  • 2022-01-21
  • 1970-01-01
  • 2011-08-04
  • 2016-04-29
  • 1970-01-01
  • 2015-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多