相关疑难解决方法(0)

使用NSLinkAttributeName在NSAttributedString中忽略Color属性

在一个NSAttributedString字母中,一系列字母具有链接属性和自定义颜色属性.

在带有Swift 2的Xcode 7中,它可以工作:

在此输入图像描述

在带有Swift 3的Xcode 8中,链接的自定义属性颜色始终被忽略(截图中应为橙色).

在此输入图像描述

这是测试代码.

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
Run Code Online (Sandbox Code Playgroud)

Swift 3,Xcode 8:

import Cocoa
import PlaygroundSupport

let text …
Run Code Online (Sandbox Code Playgroud)

cocoa nstextfield nsattributedstring swift xcode8

12
推荐指数
2
解决办法
2825
查看次数

如何更改 UILabel 中链接的颜色?

我想更改 UILabel 中链接的颜色。我发现了很多关于如何为 UITextView 执行此操作的过去问题,以及过去在 Obj-C 中有答案的问题(但无法将这些转换为 Swift,因为 Obj-c 中确实存在的属性不再执行,例如 NSMutableAttributedString .linkTextAttributes 例如)。但是我找不到如何为 UILabel 和 Swift 4 执行此操作。

ios swift

4
推荐指数
1
解决办法
6821
查看次数

标签 统计

swift ×2

cocoa ×1

ios ×1

nsattributedstring ×1

nstextfield ×1

xcode8 ×1