チャットアプリのUIを作っていて、メッセージ(Label)に空白を設定し、LINEのような吹き出しにしようとしています。
上記のように左右と上部に空白を設定できましたが、赤丸で示されているように、Labelのテキストの下に空白が設定されず、まだ吹き出しとしては未完成です。
下部の空白(padding-bottom)を設ける方法を下記で見つけましたが、
↓
Add top and bottom padding to UILabel using Swift | StackOverFlow
swift
1'UIEdgeInsetsInsetRect' has been replaced by instance method 'CGRect.inset(by:)'
というエラーの詳細がわからず、困っています。
上記のリンク先の方法、またはそれ以外の方法があれば、どうか教えてください!!
swift
1import UIKit 2import AVFoundation 3 4class SecondViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate { 5 6 @IBOutlet weak var tableView: UITableView! 7 @IBOutlet weak var textField: UITextField! 8 @IBOutlet weak var send: UIButton! 9 10 //メッセを格納する配列 11 var array = [String]() 12 13 //ラベル 14 var label = UILabel() 15 16 //パス 17 let notificationPath = Bundle.main.path(forResource: "通知オン", ofType: "mp3") 18 19 //プレイヤー 20 var notificationPlayer = AVAudioPlayer() 21 22 let paragraphStyle = NSMutableParagraphStyle() 23 24 override func viewDidLoad() { 25 super.viewDidLoad() 26 27 textField.delegate = self 28 29 //?? 30 paragraphStyle.paragraphSpacing = 18 31 //?? 32 paragraphStyle.lineHeightMultiple = 1.2 33 //?? 34 paragraphStyle.firstLineHeadIndent = 20 35 //行の始めのインデント 36 paragraphStyle.headIndent = 20 37 //行の終わりのインデント 38 paragraphStyle.tailIndent = -20 39 //行間 40 paragraphStyle.lineSpacing = 5 41// paragraphStyle.maximumLineHeight = label.font.pointSize + 10 42// paragraphStyle.minimumLineHeight = label.font.pointSize + 6 43 44 } 45 46 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 47 48 return array.count 49 50 } 51 52 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 53 54 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 55 56 label = cell.viewWithTag(1) as! UILabel 57 label.font = UIFont(name: "HiraKakuProN-W3", size: 18) 58 label.numberOfLines = 0 59 label.lineBreakMode = .byCharWrapping 60// label.text = array[indexPath.row] 61 let attributedString = NSAttributedString(string: array[indexPath.row] /* long text */, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle]) 62 label.attributedText = attributedString 63 label.layer.cornerRadius = 200 64 65 let date_label = cell.viewWithTag(2) as! UILabel 66 let f = DateFormatter() 67 f.dateStyle = .none 68 f.timeStyle = .short 69 let now = Date() 70 date_label.text = f.string(from: now) 71 72 return cell 73 74 } 75 76 77 //TextFieldでEnterが押された時、 78 func textFieldShouldReturn(_ textField: UITextField) -> Bool { 79 80 do { 81 notificationPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: notificationPath!)) 82 notificationPlayer.play() 83 } catch { 84 print("通知音でエラー発生") 85 } 86 87 //もし入力値があるなら、代入 88 if let _ = textField.text { 89 array.append(textField.text!) 90 } 91 92 //リロード 93 tableView.reloadData() 94 95 //セルが追加される度に自動スクロール 96 DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { 97 self.tableView.scrollToRow(at: IndexPath(row: self.array.count - 1, section: 0), at: UITableView.ScrollPosition.bottom, animated: true) 98 } 99 100 return true 101 102 } 103 104} 105 106class label: UILabel { 107 override func drawText(in rect: CGRect) { 108 super.drawText(in: UIEdgeInsetsInsetRect(rect, UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10))) //ここで"'UIEdgeInsetsInsetRect' has been replaced by instance method 'CGRect.inset(by:)'"というエラーが出ます。 109 } 110} 111
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/01 17:35