带有 NSAttributedString 和 NSTextAttachment 的 UITextView。如何显示文字?

Posted

技术标签:

【中文标题】带有 NSAttributedString 和 NSTextAttachment 的 UITextView。如何显示文字?【英文标题】:UITextView with NSAttributedString and NSTextAttachment. How to show text? 【发布时间】:2014-08-27 16:35:12 【问题描述】:

我创建了一个添加到 UIView 的 UITextView,这个 TextView 应该在右上角或左上角显示一个由一些文本包裹的图像。

创建 TextView 不是问题(显然),我只是无法围绕 NSAttributedString。

至于图片,我使用了 NSTextAttachment;

var image:UIImage = UIImage(named: "placeHolder.png")
var textAttachment:NSTextAttachment = NSTextAttachment(data: nil, ofType: nil)
textAttachment.image = image

然后我们有一些TextView的文本;

var text:String = "Lorem ipsum"

现在是棘手的部分。我可以创建一个可以设置为 UITextView 的 NSAttributedString。它显示图像很好,我只是不知道如何设置文本。

var s:NSAttributedString = NSAttributedString(attachment: textAttachment)
textView.attributedText = s

有什么想法吗?

【问题讨论】:

【参考方案1】:

这里的代码展示了如何构造一个由文本和一些 NSTextAttachments 组成的 NSAttributedString:

https://github.com/mattneub/Programming-ios-Book-Examples/blob/master/bk2ch10p507tabStops/TextTabTest/ViewController.swift

不过……

这个 TextView 应该在右上角或左上角显示一个被一些文本包裹的图像。

那么你想要的是不是一个NSTextAttachment,因为那是一个内联图像没有包装。

要通过环绕获取固定位置的图像,请使用图像子视图并使用排除路径进行环绕。

【讨论】:

天啊!用 NSTextAttachment Al 浪费了一个小时,做得很好。 :) 感谢@matt 的链接,将来可能会变得很方便。不过,排除路径确实看起来更有希望。 :P 我建议您观看有关 TextKit 的 WWDC 2013 视频,因为它们专门讨论了环绕固定图像。【参考方案2】:

设置图片前的文字:

var attributeString:NSMutableAttributedString = NSMutableAttributedString(string: text)
attributeString.appendAttributedString(s)
textView.attributedText = attributeString;

或者文字前的图片:

var attributeString:NSMutableAttributedString = NSMutableAttributedString(string: s)
s.appendAttributedString(text)
textView.attributedText = s

【讨论】:

【参考方案3】:

我为这个问题创建了一个示例项目。我还用stringAttributes 来写文字。

你可以参考一下。

代码如下:

import UIKit

class ViewController: UIViewController 

    @IBOutlet var label: UILabel!

    override func viewDidLoad() 
        super.viewDidLoad()

        let imageSize = CGSize(width: 20, height: 20)

        let finalAttributedString = NSMutableAttributedString(string: "")

        let birdStringAttributes = [
            NSForegroundColorAttributeName: UIColor.greenColor(),
            NSBackgroundColorAttributeName: UIColor.blueColor(),
            NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleDouble.rawValue,
            NSFontAttributeName: UIFont.systemFontOfSize(19.0)
        ]

        finalAttributedString.appendAttributedString(stringForAttachment(named: "bird", imageSize: imageSize, caption: "Something really amazing happened in Downtown Spokane this week and I had to share the story with you. Some of you may know that my brother, Joel, is a loan officer at Sterling Bank. He works downtown in a second story office building, overlooking busy Riverside Avenue.", stringAttributes: birdStringAttributes))

        let duckStringAttributes = [
            NSForegroundColorAttributeName: UIColor.orangeColor(),
            NSBackgroundColorAttributeName: UIColor.blueColor(),
            NSFontAttributeName: UIFont.systemFontOfSize(14.0)
        ]

        finalAttributedString.appendAttributedString(stringForAttachment(named: "duck", imageSize: imageSize, caption: "Several weeks ago he watched a mother duck choose the cement awning outside his window as the uncanny place to build a nest above the sidewalk.The mallard laid ten eggs in a nest in the corner of the planter that is perched over 10 feet in the air. She dutifully kept the eggs warm for weeks and Monday afternoon all of her ten ducklings hatched.", stringAttributes: duckStringAttributes))

        label.attributedText = finalAttributedString
    

    private func stringForAttachment(named imageName: String, imageSize: CGSize, caption: String, stringAttributes: [String : AnyObject]) -> NSAttributedString 
        let attachment = NSTextAttachment()
        let image = UIImage(named: imageName)
        attachment.image = image
        attachment.bounds.size = imageSize
        let fullString = NSMutableAttributedString(string: "")
        fullString.appendAttributedString(NSAttributedString(attachment: attachment))
        fullString.appendAttributedString(NSAttributedString(string: caption, attributes: stringAttributes))
        return fullString
    


结果是:

【讨论】:

以上是关于带有 NSAttributedString 和 NSTextAttachment 的 UITextView。如何显示文字?的主要内容,如果未能解决你的问题,请参考以下文章

带有 NSAttributedString(s) 的简单 UIAlertView

带有 NSLinkAttributeName 的 NSAttributedString 中的颜色属性被忽略

Swift - 带有链接的 HTML 风格化 NSAttributedString

带有多个链接的 NSAttributedString 的 UILabel,带有行限制,显示尾部截断,带有未见文本的 NSBackgroundColorAttributeName

利用NSAttributedString实现图文混排

NSAttributedString使用方法整理