我觉得我错过了一些简单的东西,但我似乎无法找到如何做到这一点:
我将属性设置为如下链接:
[myAttrString addAttribute:NSLinkAttributeName value:linkURL range:selectedRange];
Run Code Online (Sandbox Code Playgroud)
这是有效的,但链接是蓝色的,我似乎无法改变颜色.这将除了链接之外的所有内容设置为白色
[myAttrString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:selectedRange];
Run Code Online (Sandbox Code Playgroud)
是否有其他颜色属性名称,我似乎无法找到特定于链接?
我有一个问题,"boundingRectForGlyphRange"总是返回CGRect.zero"0.0,0.0,0.0,0.0"."boundingRectForGlyphRange"无效.例如,我正在编写触摸UILabel功能的部分文本.我的文字的第一部分是"任何文字",第二部分是"阅读更多".我希望点击识别器仅在我触摸"READ MORE"时才能工作.如果我触摸UILabel上的任何一点,"CGRectContainsPoint"总是返回true,然后调用该动作
这是我的代码:
override func viewDidLoad() {
super.viewDidLoad()
// The full string
let firstPart:NSMutableAttributedString = NSMutableAttributedString(string: "Lorem ipsum dolor set amit ", attributes: [NSFontAttributeName: UIFont.systemFontOfSize(13)])
firstPart.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(),
range: NSRange(location: 0, length: firstPart.length))
info.appendAttributedString(firstPart)
// The "Read More" string that should be touchable
let secondPart:NSMutableAttributedString = NSMutableAttributedString(string: "READ MORE", attributes: [NSFontAttributeName: UIFont.systemFontOfSize(14)])
secondPart.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(),
range: NSRange(location: 0, length: secondPart.length))
info.appendAttributedString(secondPart)
lblTest.attributedText = info
// Store range of chars we want to detect touches for
moreStringRange = NSMakeRange(firstPart.length, secondPart.length)
print("moreStringRange\(moreStringRange)") …Run Code Online (Sandbox Code Playgroud) 我想为链接着色,并为从 html 接收的文本中的链接下划线赋予特殊颜色。
这就是我现在所拥有的:
...
public func htmlStyleAttributeText(text: String) -> NSMutableAttributedString? {
if let htmlData = text.data(using: .utf8) {
let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html, NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue]
let attributedString = try? NSMutableAttributedString(data: htmlData, options: options, documentAttributes: nil)
let attributes: [NSAttributedString.Key: AnyObject] = [NSAttributedString.Key.foregroundColor: UIColor.red]
attributedString?.addAttributes(attributes, range: NSRange.init(location: 0, length: attributedString?.length ?? 0))
return attributedString
}
return nil
}
....
Run Code Online (Sandbox Code Playgroud)
我正在寻找的是文本的常规颜色、链接的红色和链接下划线的绿色
我尝试将一些属性设置为 NSAttributedString。除了前景色外,一切正常。
这就是我尝试设置属性的方式:
func setLabelText(text:String){
let strokeTextAttributes = [
NSAttributedString.Key.strokeColor : UIColor.white,
NSAttributedString.Key.foregroundColor : UIColor.red,
NSAttributedString.Key.font : UIFont.init(name: "Raleway-ExtraBold", size: 26)!,
NSAttributedString.Key.strokeWidth : 0.5,
]
as [NSAttributedString.Key : Any]
label.attributedText = NSMutableAttributedString(string: text, attributes: strokeTextAttributes)
}
Run Code Online (Sandbox Code Playgroud)
正如您在图像中看到的,它没有设置文本颜色:
你知道为什么它会忽略foregroundColor属性吗?
提前致谢。