标签: nsattributedstringkey

无法将NSAttributedString.DocumentAttributeKey类型的值转换为.DocumentReadingOptionKey

我在SO上找到了这个字符串扩展,允许我将html代码转换为属性字符串:

func html2AttributedString() -> NSAttributedString {
    return try! NSAttributedString(data: self.data(using: String.Encoding.unicode, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
}
Run Code Online (Sandbox Code Playgroud)

它在Swift 3中运行良好,但是对于Swift 4,Xcode抱怨:

无法将'NSAttributedString.DocumentAttributeKey'类型的值转换为预期的字典键类型'NSAttributedString.DocumentReadingOptionKey'

我该如何解决?

ios swift swift4 nsattributedstringkey

36
推荐指数
4
解决办法
2万
查看次数

无法转换'[String:AnyObject]类型的值?' 预期的参数类型'[NSAttributedStringKey:Any]?'

如何将类型的值转换'[String : AnyObject]?'为预期的参数类型'[NSAttributedStringKey : Any]?'

open class func drawText(context: CGContext, text: String, point: CGPoint, 
align: NSTextAlignment, attributes: [String : AnyObject]?)
{
    var point = point

    if align == .center
    {
        point.x -= text.size(withAttributes: attributes).width / 2.0
    }
    else if align == .right
    {
        point.x -= text.size(withAttributes: attributes).width
    }

    NSUIGraphicsPushContext(context)

    (text as NSString).draw(at: point, withAttributes: attributes)

    NSUIGraphicsPopContext()
}
Run Code Online (Sandbox Code Playgroud)

nsattributedstring swift swift4 nsattributedstringkey

20
推荐指数
2
解决办法
1万
查看次数

Swift NSAttributedStringKey没有正确应用前景色

我正在尝试创建一个meme生成器应用程序,以更好地理解Swift,Xdode,代理和UIKit.我试图使用NSAttributedStringKey将meme文本的颜色设置为白色并使用黑色笔划.黑色笔划正在工作,但当用户从相机胶卷中选择图像时,不会应用前景色.我已经搜索了StackOverflow,但所有的解决方案都适用于Swift 3,而不是Swift 4.我哪里出错?

我在下面列出了我的源代码.

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var imagePickerView: UIImageView!
    @IBOutlet weak var cameraButton: UIBarButtonItem!
    @IBOutlet weak var topText: UITextField!
    @IBOutlet weak var bottomText: UITextField!

    let memeTextAttributes:[String:Any] = [
        NSAttributedStringKey.strokeColor.rawValue: UIColor.black,
        NSAttributedStringKey.foregroundColor.rawValue: UIColor.white,
        NSAttributedStringKey.font.rawValue: UIFont(name: "HelveticaNeue-CondensedBlack", size: 40)!,
        NSAttributedStringKey.strokeWidth.rawValue: 3.0]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Diable camer a button if camera ource isn't available
        cameraButton.isEnabled = UIImagePickerController.isSourceTypeAvailable(.camera)
        topText.defaultTextAttributes = memeTextAttributes
        bottomText.defaultTextAttributes = memeTextAttributes
    }

    // MARK: Delegate Methods

    func imagePickerController(_ picker: UIImagePickerController, …
Run Code Online (Sandbox Code Playgroud)

ios swift nsattributedstringkey

8
推荐指数
2
解决办法
4291
查看次数

使用swift 4的NSAttributedStringKey错误

更新到Swift 4后,我收到此代码的错误

    attributes["NSFont"] = font
    attributes["NSColor"] = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1)
Run Code Online (Sandbox Code Playgroud)

无法使用索引类型为"String"的类型'[NSAttributedStringKey:Any]'下标值

我能够通过替换修复第一行["NSFont"],NSAttributedStringKey.font但我不知道如何修复第二行.

nsattributedstring ios swift swift4 nsattributedstringkey

8
推荐指数
2
解决办法
5358
查看次数

NSAttributedStringKey.foregroundColor无效

我正在使用ZSWTappableLabelZSWTaggedString在标签内显示链接.

import ZSWTappableLabel
import ZSWTaggedString
Run Code Online (Sandbox Code Playgroud)

pod版本是:

  pod 'ZSWTappableLabel', '~> 2.0'
  pod 'ZSWTaggedString/Swift', '~> 4.0'
Run Code Online (Sandbox Code Playgroud)

默认情况下,链接过去常常以白色显示(与标签颜色相同),但在最近发生的一些更新(可能是pod更新或xcode版本,我无法准确指出是什么)之后,链接已经开始以蓝色显示.设置NSAttributedStringKey.foregroundColor为白色似乎不会影响任何事情.NSAttributedStringKey.backgroundColor确实会影响它,但由于某种原因,foregroundColor似乎没有任何影响.

如何设置白色链接?

func setTermsAndPrivacyLinkLabel(){
    termsAndPrivacyLabel.tapDelegate = self

    let options = ZSWTaggedStringOptions()
    options["link"] = .dynamic({ tagName, tagAttributes, stringAttributes in
        guard let type = tagAttributes["type"] as? String else {
            return [NSAttributedStringKey : Any]()
        }

        var foundURL: NSURL?

        switch type {
        case "privacy":
            foundURL = NSURL(string: "\(privacyUrl)")!
        case "tos":
            foundURL = NSURL(string: "\(termsUrl)")!
        default:
            break
        }

        guard let URL …
Run Code Online (Sandbox Code Playgroud)

nsattributedstring swift swift4 nsattributedstringkey

5
推荐指数
1
解决办法
255
查看次数

无法将“NSAttributedString.Key”类型的值转换为预期的字典键类型“String”错误(swift4.2)

从 swift 3 升级到 swift 4.2 时出现以下两行代码错误

let lineattribute : [String: Any] = [
    NSForegroundColorAttributeName : UIColor(hexString: "#0f88b7ff")!,
    NSUnderlineStyleAttributeName : NSUnderlineStyle.styleSingle.rawValue
]

let attributeString = NSMutableAttributedString(string: "View travelling details", attributes: lineattribute)
Run Code Online (Sandbox Code Playgroud)

错误

ios nsattributedstringkey swift4.2

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

无法使用类型为"NSAttributedStringKey"的索引下标"[String:AnyObject]"类型的值

我在我的项目中使用下面的代码.更新到swift 4后,我收到错误.我该如何解决?

码:

let returnString : NSAttributedString

   if styleList.count > 0
   {
       var attrs = [String:AnyObject]()
       attrs[NSAttributedStringKey.font] = codeFont
       for style in styleList
       {
         if let themeStyle = themeDict[style]
         {
             for (attrName, attrValue) in themeStyle
             {
                 attrs.updateValue(attrValue, forKey: attrName)
             }
         }
     }

     returnString = NSAttributedString(string: string, attributes:attrs )
 }
Run Code Online (Sandbox Code Playgroud)

这是错误:


无法使用类型为"NSAttributedStringKey"的索引下标"[String:AnyObject]"类型的值


无法将'[String:AnyObject]'类型的值转换为预期的参数类型'[NSAttributedStringKey:Any]?'

nsattributedstring swift swift4 xcode9 nsattributedstringkey

3
推荐指数
1
解决办法
5417
查看次数

swift 4 中的 UITextField 属性占位符颜色和不透明度

我有自定义的textField具有@IBInspectable财产placeHolderColor: UIColor和正常工作。我通过以下方式设置:

attributedPlaceholder = NSAttributedString(string: placeHolder, attributes:[NSAttributedStringKey.foregroundColor: placeHolderColor])
Run Code Online (Sandbox Code Playgroud)

如何以编程方式仅为此属性设置不透明度值,而不是为文本字段中的普通文本设置不透明度值?我没有找到任何匹配NSAttributedStringKey来执行此操作

uitextfield ios swift nsattributedstringkey

3
推荐指数
1
解决办法
3714
查看次数

如何更改 AttributedString 中 Markdown 样式文本的外观?

WWDC 2021在 Foundation 中引入了新的 AttributedString API,包括 Markdown 支持。例如,我想更改通过 markdown 创建.stronglyEmphasized的文本的外观AttributedString,使强调文本的前景色为红色(以及粗体)。据我所知,它是这样的:

let attributedString = try! AttributedString(markdown: "This string *has* some markdown")
let transformed = attributedString.transformingAttributes(\.stronglyEmphasized) { attribute in
    attribute.replace(with: .foregroundColor, value: Color.red)
}
Run Code Online (Sandbox Code Playgroud)

但这告诉我“如果没有上下文类型,就无法解析对成员‘stronglyEmphasized’的引用”并且“无法推断通用参数‘U’”。这是毫无帮助的。

macos foundation nsattributedstring ios nsattributedstringkey

3
推荐指数
1
解决办法
1215
查看次数

在macOS中设置'foregroundColor'时出错

在使用Swift 3.2的macOS项目中,我正在尝试设置UITextView的前景色.

let placeHolderTitleString: NSAttributedString = NSAttributedString(string: "Enter text here", attributes: [NSAttributedStringKey.foregroundColor : NSColor.gray]) // error
Run Code Online (Sandbox Code Playgroud)

我得到的错误是这样的:

类型'NSAttributedStringKey'(又名'NSString')没有成员'foregroundColor'

相同的代码在Swift 4.0项目中工作正常.

我试图根据我发现的iOS Swift 4转换错误的一些答案修改代码- NSAttributedStringKey:任何但我继续得到错误.有没有办法解决这个问题,而无需将项目更新到Swift 4?

macos nsattributedstring swift nsattributedstringkey

2
推荐指数
1
解决办法
744
查看次数

如何使可点击的 NSAttributedString 移动到另一个 ViewController Swift

我正在开发一个简单的应用程序并以编程方式添加NSAttributedString. 我真的很困惑如何将礼物viewcontroller转移到另一个viewcontroller

这不是重复的问题,因为我的问题是如何移动到视图控制器和重复问题如何在单击时打开链接 如何在 NSAttributedString 中创建可点击的链接?.

截屏

截屏

在上图中显示了三个,NSAttributedString第一个是dhiman.sham1gmail.com,第二个是has started following,第三个Sham。尝试单击时Sham不执行任何操作。我想在单击时将此控制器移动到另一个控制器Sham

这是我的代码:

@IBOutlet weak var descriptionLbl: UILabel!

override func updateWithModel(_ model: AnyObject) {
 self.descriptionLbl.attributedText = self.followingGetTextFromType(data: (model as? FollowingNotificationData)!)
}

func followingGetTextFromType(data:FollowingNotificationData) -> NSMutableAttributedString{
  var attributedString = NSMutableAttributedString()
  attributedString = NSMutableAttributedString(string:(data.detailsNoti?.fullName)!, attributes: strres)
  let startedfollowing = NSMutableAttributedString(string:" has started following ", attributes: attrs)
  attributedString.append(startedfollowing)
  var discription = NSMutableAttributedString()
  if data.fullName == ""{ …
Run Code Online (Sandbox Code Playgroud)

uiviewcontroller nsattributedstring ios swift nsattributedstringkey

2
推荐指数
1
解决办法
1985
查看次数

如何设置 NSAttributedString 文本颜色?

我尝试将一些属性设置为 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属性吗?

提前致谢。

nsattributedstring ios swift nsattributedstringkey

0
推荐指数
1
解决办法
3274
查看次数