NSAttributedString:图像未显示在 UILabel 中
NSAttributedString: image not showing up in UILabel
由于某种原因,图像没有显示。我已经在 Image.xcassets/ 下删除了 test.png 和 test@2x.png 我也在项目中删除了它。 None 其中有帮助。如有任何帮助,我们将不胜感激。
var testView: UILabel = UILabel(frame: CGRectMake(20, 20, self.view.frame.width-40, self.view.frame.height-40))
var a = "Testing..."
var attachment = NSTextAttachment()
//This is the image that I would like to add
// I have dropped test.png and test@2x.png under Image.xcassets
attachment.image = UIImage(named: "test")
var attachmentString = NSAttributedString(attachment: attachment)
let attrsB = NSAttributedString(
string: "2nd line..Testing testing",
attributes: NSDictionary(
object: UIFont(name: "HelveticaNeue", size: 18.0)!,
forKey: NSFontAttributeName) as [NSObject : AnyObject])
a.appendAttributedString(attrsB)
let attributedTextToDisplay = NSMutableAttributedString(attributedString: a)
attributedTextToDisplay.appendAttributedString(attachmentString)
testView.attributedText = attributedTextToDisplay
self.view.addSubview(testView)
如果要显示多于 1 行,则需要允许标签有多行。
var testView: UILabel = UILabel(frame: CGRectMake(20, 20, self.view.frame.width-40, self.view.frame.height-40))
testView.numberOfLines = 0;
提示:
- 使用故事板和界面生成器。所见即所得。
- 使用AutoLayout,它为你解决了很多调整大小的问题
- 如果您不确定变量的内容,请使用 asserts
- 进入调试器并按下眼睛图标以显示 quicklook
- 消除所有这些变量将帮助您找出根本原因,而不用担心图像是否已加载。请参阅下面的示例。
为了好的措施,我还明确地将 a
键入 a:NSMutableAttributedString
。
var a:NSMutableAttributedString
由于某种原因,图像没有显示。我已经在 Image.xcassets/ 下删除了 test.png 和 test@2x.png 我也在项目中删除了它。 None 其中有帮助。如有任何帮助,我们将不胜感激。
var testView: UILabel = UILabel(frame: CGRectMake(20, 20, self.view.frame.width-40, self.view.frame.height-40))
var a = "Testing..."
var attachment = NSTextAttachment()
//This is the image that I would like to add
// I have dropped test.png and test@2x.png under Image.xcassets
attachment.image = UIImage(named: "test")
var attachmentString = NSAttributedString(attachment: attachment)
let attrsB = NSAttributedString(
string: "2nd line..Testing testing",
attributes: NSDictionary(
object: UIFont(name: "HelveticaNeue", size: 18.0)!,
forKey: NSFontAttributeName) as [NSObject : AnyObject])
a.appendAttributedString(attrsB)
let attributedTextToDisplay = NSMutableAttributedString(attributedString: a)
attributedTextToDisplay.appendAttributedString(attachmentString)
testView.attributedText = attributedTextToDisplay
self.view.addSubview(testView)
如果要显示多于 1 行,则需要允许标签有多行。
var testView: UILabel = UILabel(frame: CGRectMake(20, 20, self.view.frame.width-40, self.view.frame.height-40))
testView.numberOfLines = 0;
提示:
- 使用故事板和界面生成器。所见即所得。
- 使用AutoLayout,它为你解决了很多调整大小的问题
- 如果您不确定变量的内容,请使用 asserts
- 进入调试器并按下眼睛图标以显示 quicklook
- 消除所有这些变量将帮助您找出根本原因,而不用担心图像是否已加载。请参阅下面的示例。
为了好的措施,我还明确地将 a
键入 a:NSMutableAttributedString
。
var a:NSMutableAttributedString