Swift4
xcode9.2
FacebookライクなFeedをコードで作っています。
let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing = 4
下記クラスFeedControllerで作成したcell内の、
ユーザーネームである"ゴールデン"と
日付け 所在地である"\nDecember 18 • san Francisco • "の間に
縦スペースを入れたいのですが、いくら4の数値を調整しても入りません。
\nをもう1つ増やしてみたりと試していますが、原因が分かりません。
ご存知の方がいらっしゃいましたら、教えていただきたいです。
// IPhone 6sでのサイズにしてます import UIKit let cellId = "cellId" var layout = UICollectionViewFlowLayout() class FeedController: UICollectionViewController, UICollectionViewDelegateFlowLayout { override func viewDidLoad() { super.viewDidLoad() //自分のナビバー タイトル navigationItem.title = "Facebook Feed" //垂直にバウンスする リロードの操作の動き collectionView?.alwaysBounceVertical = true collectionView?.backgroundColor = UIColor(white: 0.95, alpha: 1) collectionView?.register(FeedCell.self, forCellWithReuseIdentifier: cellId) } //コレクション セル 数 override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 3 } //2/6解決。エラー "method does not override from its superclass" 。書き込み直し予測変換のまま、打ち直すことで解決。 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { return collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath as IndexPath) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { // 2/8 これはラッパー関数と呼ばれるもの? return CGSize(width: view.frame.width, height: 60) } } class FeedCell: UICollectionViewCell { override init(frame: CGRect) { super.init(frame: frame) setupViews() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } //セル ユーザーネームだよ let nameLabel: UILabel = { let label = UILabel() label.numberOfLines = 2 let attributedText = NSMutableAttributedString(string: "ゴールデン", attributes: [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 14)]) //日付け 文字の色 attributedText.append(NSAttributedString(string: "\nDecember 18 • san Francisco • ", attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 12), NSAttributedStringKey.foregroundColor: UIColor(red: 155/255, green: 161/255, blue: 171/255, alpha: 1)])) //スペース パラグラフスタイル 1部misspellだと正しいspellの部分が干渉してエラーとなり、誤ったfixをしてしまう可能あり 注意する 現在はNSAttributedStringKeyという型 let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing = 4 attributedText.addAttribute(NSAttributedStringKey.paragraphStyle, value: NSParagraphStyle(), range: NSMakeRange(0, attributedText.string.count))//string前のcharacters.を消した label.attributedText = attributedText return label }() //ユーザーのプロフ画像だよ let profileImageView: UIImageView = { let imageView = UIImageView() imageView.image = UIImage(named: "hgAiYyPhkHdBRKR1518078195_1518078228") imageView.contentMode = .scaleAspectFit return imageView }() //バックグラウンド 色とか? func setupViews() { backgroundColor = UIColor.white addSubview(nameLabel) addSubview(profileImageView) // 引数Format呼び出す? Hです (44)は 横 サイズ。 これを縦横に設定してsquareにしてる addConstraintsWithFormat(format: "H:|-8-[v0(44)]-8-[v1]|", views: profileImageView, nameLabel) //60でnameLabelの位置を中央よりにしてる。まだ60で正確な中央に設定されてるかわからないから、設定できるようにする addConstraintsWithFormat(format: "V:|[v0(57)]", views: nameLabel) //-8-を減らせば上に 増やせば下にsquareの位置が変わる 8は中央 addConstraintsWithFormat(format: "V:|-8-[v0(44)]", views: profileImageView) } } extension UIView { func addConstraintsWithFormat(format: String, views: UIView...) { var viewsDictionary = [String: UIView]() for (index, view) in views.enumerated() { let key = "v(index)" viewsDictionary[key] = view view.translatesAutoresizingMaskIntoConstraints = false } addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: viewsDictionary)) } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/02/09 05:57
退会済みユーザー
2018/02/09 05:59
2018/02/09 06:03
退会済みユーザー
2018/02/09 06:07
退会済みユーザー
2018/02/09 06:19
2018/02/09 06:25
退会済みユーザー
2018/02/09 06:45