我正在处理 照片编辑应用程序我正在尝试根据 Text View 中给出的 Text 调整标签。我尝试了很多代码,但它对我不起作用。我想在图像上添加标签,该标签基于我的 textview 中给出的文本,如下图所示。 我无法根据 textview 的文本设置标签的宽度和高度。我试过以下代码。请有人帮忙。
let newWidth: CGFloat = CGFloat(txtView.text.boundingRect(with: txtView.frame.size, options: .usesLineFragmentOrigin, context: nil).size.width)
let Label = UILabel(frame: CGRect(x: 100, y: 100, width: newWidth, height: 100))
imgView?.addSubview(Label)
我也试过
var maximumLabelSize = CGSize(width: 296, height: FLT_MAX)
var expectedLabelSize: CGSize = yourString.size(withFont: yourLabel.font, constrainedTo: maximumLabelSize, lineBreakMode: yourLabel.lineBreakMode)
//adjust the label the the new height.
var newFrame: CGRect = yourLabel.frame
newFrame.size.height = expectedLabelSize.height
yourLabel.frame = newFrame
最佳答案
我已经使用 解决了我的问题JLStickerTextView 但在此存储库中,它与 不兼容 swift 3.0 所以我对项目进行了更改,您可以从她https://github.com/khush004/StickerView/tree/master 中找到工作项目而没有任何错误
关于ios - 根据swift 3中 TextView 中给出的文本调整标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46731516/