Skip to content

Instantly share code, notes, and snippets.

@jamonholmgren
Last active December 11, 2015 22:18
Show Gist options
  • Save jamonholmgren/4668372 to your computer and use it in GitHub Desktop.
Save jamonholmgren/4668372 to your computer and use it in GitHub Desktop.
def banner_view
@banner_view ||= begin
width = self.view.bounds.size.width
image = User.current_user.profile_image_view(67, 35)
image.frame = CGRectMake((width/2) - (67/2), 12, 67, 67)
BannerView.alloc.init(frame: self.view.frame, title: User.current_user.fullName, remote_image: image)
end
end
@danielberkompas
Copy link

How about this?

def banner_view
  @banner_view ||= BannerView.alloc.initWithImageAndTitle(
     image: User.current_user.profile_image_view(67, 35),
     title:    User.current_user.fullName,
     width:  self.view.bounds.size.width
   )
end

Then in the BannerView class, something like:

class BannerView < UIView
  def self.initWithImageAndTitle(image: image, title: title, width: width)
      image = User.current_user.profile_image_view(67, 35)
      image.frame = CGRectMake((width/2) - (67/2), 12, 67, 67)
      BannerView.alloc.init(frame: self.view.frame, title: User.current_user.fullName, remote_image: image)
  end
end

This wouldn't help if you didn't instantiate BannerView with images much, but if you did you might find the above more DRY.

@jamonholmgren
Copy link
Author

I think the image and image.frame part are too "caller-specific" to bake it into the BannerView, though.

@jamonholmgren
Copy link
Author

  def init(args={})
    args ||= {}
    if args[:frame]
      width = args[:frame].size.width
    else
      width = App.bounds.size.width 
    end
    top = args[:top]
    top ||= 0

    self.initWithFrame(CGRectMake(0, top, width, 120))

    args[:banner_image] ||= "assets/pattern"
    args[:mask] ||= "assets/pic-mask2"
    args[:title] ||= ""
    args[:icon] ||= "assets/ico-about" unless args[:remote_image]

    banner_view = set_attributes UIView.alloc.initWithFrame(CGRectMake(0, 2, width, 44)), {
      backgroundColor: ImageHelper.backgroundPatternFromResource(args[:banner_image])
    }
    banner_view.autoresizingMask = UIViewAutoresizingFlexibleWidth

    pic_mask_view = ImageHelper.imageViewFromResource(args[:mask], {top: 10, center_x: (width/2)})
    pic_mask_view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin

    if args[:icon]
      icon_view = ImageHelper.imageViewFromResource(args[:icon], {top:12, center_x: (width/2), radius: 35})
    elsif args[:remote_image]
      icon_view = args[:remote_image]
    end
    icon_view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin if icon_view

    title_view = set_attributes UILabel.alloc.initWithFrame(CGRectMake(0, 90, width, 30)), { 
      backgroundColor: UIColor.clearColor,
      text: args[:title],
      font: UIFont.boldSystemFontOfSize(18),
      textAlignment: UITextAlignmentCenter
    }
    title_view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleWidth

    self.addSubview banner_view
    self.addSubview icon_view if icon_view
    self.addSubview pic_mask_view
    self.addSubview title_view

    self.autoresizingMask = UIViewAutoresizingFlexibleWidth

    self
  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment