Skip to content

Instantly share code, notes, and snippets.

@morizotter
Last active October 20, 2015 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morizotter/06f25972eed87bc5bc29 to your computer and use it in GitHub Desktop.
Save morizotter/06f25972eed87bc5bc29 to your computer and use it in GitHub Desktop.
Cellなどの高さ計算の処理をViewのstructで行う ref: http://qiita.com/morizotter/items/205edf75d6da0a7f9c11
final class UserCell: UITableViewCell {
struct Layout {
static func height(vm: UserViewModel) -> CGFloat {
return 68.0
}
}
...
}
let vm = ...
return UserCell.Layout.height(vm)
final class ItemCell: UITableViewCell {
struct Layout {
static let LeftAreaMargin: CGFloat = 67.0
static let RightAreaMargin: CGFloat = 14.0
static let TopAreaHeight: CGFloat = 41.0
static let NameFont = UIFont.boldSystemFontOfSize(12)
static let TitleTopHeight: CGFloat = 8.0
static var TitleMaximumSize = CGSize(width: UIScreen.mainScreen().bounds.width - Layout.LeftAreaMargin - Layout.RightAreaMargin, height: CGFloat.max)
static let TitleFont = UIFont.boldSystemFontOfSize(16.0)
static let TitleAttributes = [NSFontAttributeName: Layout.TitleFont]
static let BottomHeight: CGFloat = 18.0
static func height(vm: ItemViewModel) -> CGFloat {
return height(NSAttributedString(string: vm.title.value, attributes: Layout.TitleAttributes))
}
static func height(attributedTitle: NSAttributedString) -> CGFloat {
var height: CGFloat = 0.0
height += Layout.TopAreaHeight
height += Layout.TitleTopHeight
height += Layout.TitleHeight(attributedTitle)
height += Layout.BottomHeight
return ceil(height)
}
static func TitleHeight(attributedTitle: NSAttributedString) -> CGFloat {
let options : NSStringDrawingOptions = .UsesLineFragmentOrigin | .UsesFontLeading
let rect: CGRect = attributedTitle.boundingRectWithSize(Layout.TitleMaximumSize, options: options, context: nil)
return ceil(rect.size.height)
}
}
...
}
let vm = ...
return ItemCell.Layout.height(vm)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment