2017-10-13 111 views
1

我正在研究照片編輯應用我試圖根據文本視圖中給出的文本來調整標籤。我已經嘗試了很多代碼,但它並沒有爲我工作。我想在圖像上添加標籤,這是基於我的textview中給出的文本,如下圖所示。 enter image description here我無法根據textview的文本設置標籤的寬度和高度。我試過了下面的代碼。有人請幫忙。根據文本視圖中的文本在swift 3中調整標籤?

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 
+1

它不完全清楚你在做什麼。你有'UIImageView',你想在它上面顯示一個'UILabel'?文本應該居中嗎?它應該覆蓋整個圖像還是隻包含一部分?我建議你展示一下你想要的結果。 – DonMag

+0

@DonMag請檢查我更新的問題。 –

+0

您提供的屏幕截圖我想它來自** JLStickerTextView **。你想添加標籤到圖片上,這個JLStickerTextView庫就是這樣做的。我正在使用這個庫進行視頻編輯應用程序。檢查作者提供的示例與庫 –

回答

0
func calcheight(strin:String) -> CGFloat 
    { 
     let label = UILabel(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: CGFloat.greatestFiniteMagnitude)) 
     label.numberOfLines = 0 
     label.text = strin 
     label.sizeToFit() 
     return label.frame.height + 2 

    } 
相關問題