如何解决在多行字符串旁边垂直对齐NSTextAttachment
我需要创建此图像:
我正在这样创建附件:
let title = "Donuts are Awesomeeee"
let range = title.range(of: "[homer]")!
let font = UIFont.boldSystemFont(ofSize: 14)
let imageSize = CGSize(width: 25,height: 25)
let imageAttachment = NSTextAttachment()
imageAttachment.image = #imageLiteral(resourceName: "homer")
let boundsOrigin = CGPoint(x: 0,y: (font.capHeight - imageSize.height) / 2)
imageAttachment.bounds = CGRect(origin: boundsOrigin,size: imageSize)
然后我添加字符串和附件:
let attributedString = NSMutableAttributedString(string: title,attributes: nil)
attributedString.insert(NSAttributedString(attachment: imageAttachment),at: 0)
该attributedString设置为UILabel
:
let label = UILabel()
label.font = font
label.textColor = .black
label.attributedText = attributedString
label.numberOfLines = 2
label.backgroundColor = .lightGray
将标签添加到子视图并设置宽度为100时,我得到:
最糟糕的是,文本的第二行位于图标的 下。那不应该发生。 知道我该怎么做吗?文本也是动态的,因此也可以是5行。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。