通过NSAttributedStringKey的附件不可见

时间:2017-10-26 11:24:29

标签: ios objective-c swift nstextattachment

新的大型标题功能可以通过largeTitleTextAttributes自定义,(与任何其他属性一样) dictionary NSAttributedStringKey个键。其中一个键是NSAttachmentAttributeName / attachment

考虑一下:

let attachment = NSTextAttachment()
attachment.image = UIImage(named: "foo")
attachment.bounds = CGRect(x: 0.0, y: 0.0, width: 20.0, height: 20.0)

var largeTitleTextAttributes: [NSAttributedStringKey: Any] = [:]
largeTitleTextAttributes[.attachment] = attachment
navigationBar.largeTitleTextAttributes = largeTitleTextAttributes
  

问题是我分配给largeTitleTextAttributes属性attachment 的附件不可见

如何将附件添加到属性词典中以便附件可见? (我不是在寻找NSAttributedString' s { {1}})

2 个答案:

答案 0 :(得分:1)

AS Apple的Doc说你只能指定

  

您可以指定字体,文本颜色,文本阴影颜色和文本   使用的文本属性字典中标题的阴影偏移量   NSAttributedStringKey中描述的文本属性键。

但你可以直接将UILabel设置为导航栏的标题视图 使用以下代码

let image1Attachment = NSTextAttachment()
        image1Attachment.image = UIImage(named: "bb")
        image1Attachment.bounds = CGRect.init(x: 0.0, y: 0.0, width: 20, height: 20)
        let image1String = NSAttributedString(attachment: image1Attachment)
        let label: UILabel = UILabel.init(frame: (self.navigationController?.navigationBar.frame)!)
        label.attributedText = image1String
        if #available(iOS 11.0, *) {
            self.navigationItem.titleView = label
            }
        else {
            // Fallback on earlier versions
        }

enter image description here

答案 1 :(得分:0)

通过查看Apple的文档,您可以在titleTextAttributess中指定的属性列表似乎有限:

  

您可以指定字体,文本颜色,文本阴影颜色和文本   使用的文本属性字典中标题的阴影偏移量   NSAttributedStringKey中描述的文本属性键。

https://developer.apple.com/documentation/uikit/uinavigationbar/1624953-titletextattributes

可悲的是,图片附件不在列表中。

相关问题