在NSAttributedString中使用NSLinkAttributeName忽略颜色属性 [英] Color attribute is ignored in NSAttributedString with NSLinkAttributeName

查看:5787
本文介绍了在NSAttributedString中使用NSLinkAttributeName忽略颜色属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NSAttributedString 中,字母范围具有链接属性和自定义颜色属性。



在Xcode 7和Swift 2中,它的工作原理:





这里是测试代码。



Swift 2,Xcode 7:

  import Cocoa 
import XCPlayground

let text =Hey @user!

let attr = NSMutableAttributedString(string:text)
let range = NSRange(location:4,length:5)
attr.addAttribute(NSForegroundColorAttributeName,value:NSColor.orangeColor ),范围:range)
attr.addAttribute(NSLinkAttributeName,value:http://somesite.com/,range:range)

let tf = NSTextField(frame:NSRect x:0,y:0,width:200,height:50))
tf.allowsEditingTextAttributes = true
tf.selectable = true
tf.stringValue = text
tf。 attributStringValue = attr

XCPlaygroundPage.currentPage.liveView = tf

Swift 3, Xcode 8:

  import Cocoa 
import PlaygroundSupport

let text =Hey @user !

let attr = NSMutableAttributedString(string:text)
let range = NSRange(location:4,length:5)
attr.addAttribute(NSForegroundColorAttributeName,value:NSColor.orange,范围:范围)
attr.addAttribute(NSLinkAttributeName,value:http://somesite.com/,range:range)

let tf = NSTextField(frame:NSRect 0,y:0,width:200,height:50))
tf.allowsEditingTextAttributes = true
tf.isSelectable = true
tf.stringValue = text
tf.attributedStringValue = attr

PlaygroundPage.current.liveView = tf

错误报告给苹果,但在此期间,如果有人有一个想法的解决或解决方案在Xcode 8,这将是巨大的。

解决方案

Apple开发人员已回答:


请注意,我们的工程小组已确定此问题


并且他们解释为什么它以前工作,但不再:

$ b $不幸的是,之前的行为(以自定义颜色显示NSLinkAttributeName的属性字符串范围)未明确支持 。它发生了工作,因为NSTextField只有呈现链接时,字段编辑器存在;没有字段编辑器,我们回到由NSForegroundColorAttributeName指定的颜色。



版本10.12更新了NSLayoutManager和NSTextField,使用默认链接外观来呈现链接,类似于iOS。 (请参阅AppKit 10.12版发行说明。 )



为了提高一致性,预期的行为是表示使用默认链接外观绘制的链接(通过NSLinkAttributeName指定)的范围。因此,目前的行为是预期的行为。


b

In an NSAttributedString, a range of letters has a link attribute and a custom color attribute.

In Xcode 7 with Swift 2, it works:

In Xcode 8 with Swift 3, the custom attributed color for the link is always ignored (it should be orange in the screenshot).

Here's the code for testing.

Swift 2, Xcode 7:

import Cocoa
import XCPlayground

let text = "Hey @user!"

let attr = NSMutableAttributedString(string: text)
let range = NSRange(location: 4, length: 5)
attr.addAttribute(NSForegroundColorAttributeName, value: NSColor.orangeColor(), range: range)
attr.addAttribute(NSLinkAttributeName, value: "http://somesite.com/", range: range)

let tf = NSTextField(frame: NSRect(x: 0, y: 0, width: 200, height: 50))
tf.allowsEditingTextAttributes = true
tf.selectable = true
tf.stringValue = text
tf.attributedStringValue = attr

XCPlaygroundPage.currentPage.liveView = tf

Swift 3, Xcode 8:

import Cocoa
import PlaygroundSupport

let text = "Hey @user!"

let attr = NSMutableAttributedString(string: text)
let range = NSRange(location: 4, length: 5)
attr.addAttribute(NSForegroundColorAttributeName, value: NSColor.orange, range: range)
attr.addAttribute(NSLinkAttributeName, value: "http://somesite.com/", range: range)

let tf = NSTextField(frame: NSRect(x: 0, y: 0, width: 200, height: 50))
tf.allowsEditingTextAttributes = true
tf.isSelectable = true
tf.stringValue = text
tf.attributedStringValue = attr

PlaygroundPage.current.liveView = tf

I've sent a bug report to Apple, but in the meantime if someone has an idea for a fix or workaround in Xcode 8, that would be great.

解决方案

Apple Developer has answered:

Please know that our engineering team has determined that this issue behaves as intended based on the information provided.

And they explain why it worked before but doesn't anymore:

Unfortunately, the previous behavior (attributed string ranges with NSLinkAttributeName rendering in a custom color) was not explicitly supported. It happened to work because NSTextField was only rendering the link when the field editor was present; without the field editor, we fall back to the color specified by NSForegroundColorAttributeName.

Version 10.12 updated NSLayoutManager and NSTextField to render links using the default link appearance, similar to iOS. (see AppKit release notes for 10.12.)

To promote consistency, the intended behavior is for ranges that represent links (specified via NSLinkAttributeName) to be drawn using the default link appearance. So the current behavior is the expected behavior.

(emphasis mine)

这篇关于在NSAttributedString中使用NSLinkAttributeName忽略颜色属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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