Skip to content

Ham Button (Swift)

Weiping Huang edited this page May 13, 2017 · 5 revisions

Add ham buttons with with a text, sub-text and image inside for each to BMB.

Swift | Objective-C

Create BMB

As mentioned in Basic Usage, BMB must know the kind of boom-buttons and how it should place the boom-buttons on itself and screen.

bmb.buttonEnum = .ham
bmb.piecePlaceEnum = .ham_3
bmb.buttonPlaceEnum = .ham_3

For more information and pictures about piece-place-enum and button-place-enum, check the lists below.

Add Builders

The builder of ham button has lots of methods to customize the boom-buttons. Needn't to set every attributes for boom-buttons, just customize what you want. For example:

for _ in 0..<bmb.piecePlaceEnum.pieceNumber() {
    let builder = HamButtonBuilder.init()
    builder.normalImageName = "butterfly"
    builder.normalText = "Text"
    builder.normalSubText = "Sub Text"
    bmb.addBuilder(builder)
}

In the above loop, the value of bmb.piecePlaceEnum.pieceNumber() is 3, because the piecePlaceEnum is set to .ham_3. So we provide 3 builders for the 3 boom-buttons, with the same image butterfly, same text Text and same sub-text Sub Text.

If you wanna customize more properties for boom-buttons:

for _ in 0..<bmb.piecePlaceEnum.pieceNumber() {
    let builder = HamButtonBuilder.init()
    
    // Event closure when the boom-button corresponding the builder itself is clicked.
    builder.clickedClosure = { (index: Int) -> Void in
        // the boom-button is clicked
    }
    
    // Whether the boom-button is unable (for click event). The default value is false.
    builder.unable = false
    
    // Whether the image in boom-button plays a rotation animation.
    builder.rotateImage = false

Piece

    // The color of the corresponding piece. 
    // If the color of piece is nil (by default), then BMB will use the color of boom-button 
    // at normal state (if the boom-buttons is unable, then use color at unable state) to draw the piece. 
    // If the color of piece is different from boom-button's, then BMB will play a discoloration 
    // animation when booming or rebooming.
    builder.pieceColor = UIColor.white

Shadow

    // Whether the boom-button has a shadow effect.
    builder.hasShadow = true
    
    // The rect of shadow path of boom-button.
    builder.shadowPathRect = CGRect.init(x: 2, y: 2, width: 76, height: 76)
    
    // The offset (in points) of shadow in x axis.
    builder.shadowOffsetX = 0
    
    // The offset (in points) of shadow in y axis.
    builder.shadowOffsetY = 8
    
    // The blur radius (in points) used to render the boom-button's shadow.
    builder.shadowRadius = 4
    
    // The color of the boom-button's shadow.
    builder.shadowColor = UIColor.darkGray

Image

    // The name of the image on the boom-button at normal state.
    builder.normalImageName = "butterfly"
    
    // The image on the boom-button at normal state.
    builder.normalImage = UIImage.init(named: "butterfly")
    
    // The name of the image on the boom-button at highlighted state.
    builder.highlightedImageName = "bear"
    
    // The image on the boom-button at highlighted state.
    builder.highlightedImage = UIImage.init(named: "bear")
    
    // The name of the image on the boom-button at unable state.
    builder.unableImageName = "bat"
    
    // The image on the boom-button at unable state.
    builder.unableImage = UIImage.init(named: "bat")
    
    // The tint color of image on boom-button at normal state.
    builder.normalImageTintColor = UIColor.red
    
    // The tint color of image on boom-button at highlighted state.
    builder.highlightedImageTintColor = UIColor.yellow
    
    // The tint color of image on boom-button at unable state.
    builder.unableImageTintColor = UIColor.blue
    
    // The frame of the image view on boom-button.
    builder.imageFrame = CGRect.init(x: 10, y: 10, width: 60, height: 60)
    
    // The image size of the image view on boom-button.
    builder.imageSize = CGSize.init(width: 60, height: 60)

Text

    // The text on boom-button at normal state.
    builder.normalText = "Text Inside"
    
    // The text on boom-button at highlighted state.
    builder.highlightedText = "Highlighted Text"

    // The text on boom-button at unable state.
    builder.unableText = "Unable"

    // The attributed text on boom-button at normal state.
    builder.normalAttributedText = NSAttributedString.init(string: "Text")

    // The attributed text on boom-button at highlighted state.
    builder.highlightedAttributedText = NSAttributedString.init(string: "Text")

    // The attributed text on boom-button at unable state.
    builder.unableAttributedText = NSAttributedString.init(string: "Text")

    // The color of text on boom-button at normal state.
    builder.normalTextColor = UIColor.white

    // The color of text on boom-button at highlighted state.
    builder.highlightedTextColor = UIColor.yellow

    // The color of text on boom-button at unable state.
    builder.unableTextColor = UIColor.darkGray

    // The frame of text on boom-button.
    builder.textFrame = CGRect.init(x: 15, y: 52, width: 50, height: 20)

    // The font of text on boom-button.
    builder.textFont = UIFont.systemFont(ofSize: 10)

    // The alignment of text on boom-button.
    builder.textAlignment = NSTextAlignment.center

    // The line break mode of text on boom-button.
    builder.textLineBreakMode = NSLineBreakMode.byCharWrapping

    // The maximum number of lines to use for text on boom-button.
    builder.textLines = 2

    // The color of shadow for text on boom-button.
    builder.textShadowColor = UIColor.darkGray

    // The shadow offset (in points) on x axis for text on boom-button.
    builder.textShadowOffsetX = 2

    // The shadow offset (in points) on y axis for text on boom-button.
    builder.textShadowOffsetY = 2

Sub Text

    // Whether the boom-button contains a sub-text. All properties for sub-text only work when "containsSubText" is true.
    builder.containsSubText = true
            
    // The sub-text on boom-button at normal state.
    builder.normalSubText = "Sub Text"
            
    // The sub-text on boom-button at highlighted state.
    builder.highlightedSubText = "Sub Text"
            
    // The sub-text on boom-button at unable state.
    builder.unableSubText = "Sub Text"
            
    // The attributed sub-text on boom-button at normal state.
    builder.normalAttributedSubText = NSAttributedString.init(string: "Text")
            
    // The attributed sub-text on boom-button at highlighted state.
    builder.highlightedAttributedSubText = NSAttributedString.init(string: "Text")
            
    // The attributed sub-text on boom-button at unable state.
    builder.unableAttributedSubText = NSAttributedString.init(string: "Text")
            
    // The color of sub-text on boom-button at normal state.
    builder.normalSubTextColor = UIColor.white
            
    // The color of sub-text on boom-button at highlighted state.
    builder.highlightedSubTextColor = UIColor.yellow
            
    // The color of sub-text on boom-button at unable state.
    builder.unableSubTextColor = UIColor.darkGray
            
    // The frame of sub-text on boom-button.
    builder.subTextFrame = CGRect.init(x: 70, y: 35, width: 220, height: 15)
            
    // The font of sub-text on boom-button.
    builder.subTextFont = UIFont.systemFont(ofSize: 9)
            
    // The alignment of sub-text on boom-button.
    builder.subTextAlignment = NSTextAlignment.left
            
    // The line break mode of sub-text on boom-button.
    builder.subTextLineBreakMode = NSLineBreakMode.byTruncatingTail
            
    // The maximum number of lines to use for sub-text on boom-button.
    builder.subTextLines = 1
            
    // The color of shadow for sub-text on boom-button.
    builder.subTextShadowColor = UIColor.darkGray
            
    // The shadow offset (in points) on x axis for sub-text on boom-button.
    builder.subTextShadowOffsetX = 2
            
    // The shadow offset (in points) on y axis for sub-text on boom-button.
    builder.subTextShadowOffsetY = 2

Button

    // The color of boom-button at normal state.
    builder.normalColor = UIColor.white
    
    // The color of boom-button at highlighted state.
    builder.highlightedColor = UIColor.green
    
    // The color of boom-button at unable state.
    builder.unableColor = UIColor.black
    
    // The corner radius (in points) of boom-button.
    builder.cornerRadius = 5
    
    // Width (in points) of the boom-button (including the text label).
    builder.width = 300

    // Height (in points) of the boom-button (including the text label).
    builder.height = 60

    bmb.addBuilder(builder)
}

Piece Place Enum for Ham Button

All piece-place-enum and button-place-enum can be found in demo.

  1. PiecePlaceEnum.ham_1
  2. PiecePlaceEnum.ham_2
  3. PiecePlaceEnum.ham_3
  4. PiecePlaceEnum.ham_4
  5. PiecePlaceEnum.ham_5
  6. PiecePlaceEnum.ham_6
  7. PiecePlaceEnum.custom
    PiecePlaceEnum.custom enables developers to customize the number and the positions of boom-buttons. Check the demo and wiki for more information.

Button Place Enum for Ham Button

All piece-place-enum and button-place-enum can be found in demo.

  1. ButtonPlaceEnum.ham_1
  2. ButtonPlaceEnum.ham_2
  3. ButtonPlaceEnum.ham_3
  4. ButtonPlaceEnum.ham_4
  5. ButtonPlaceEnum.ham_5
  6. ButtonPlaceEnum.ham_6
  7. ButtonPlaceEnum.horizontal
    ButtonPlaceEnum.horizontal place all the boom-buttons in a horizontal line.
  8. ButtonPlaceEnum.vertical
    ButtonPlaceEnum.vertical place all the boom-buttons in a vertical line.
  9. ButtonPlaceEnum.custom
    ButtonPlaceEnum.custom enables developers to customize the number and the positions of boom-buttons. Check the demo and wiki for more information.

The Special Boom-Button

Sometimes we need to add a boom-button with "Cancel" on the last boom-button(the bottom one). And maybe the last one should have a larger top-margin with its above one. Then you can set this special attribute by:

bmb.buttonHamButtonTopMargin = 30

Home
Chapters

  1. Basic Usage
  2. Simple Circle Button
  3. Text Inside Circle Button
  4. Text Outside Circle Button
  5. Ham Button
  6. Share Style
  7. Custom Position
  8. Button Place Alignments
  9. Different Ways to Boom
  10. Ease Animations for Buttons
  11. Different Order for Buttons
  12. Other Animations Attributes for Buttons
  13. Click Event and Listener
  14. Control BMB
  15. Use BMB in Navigation Bar
  16. Use BMB in Table View
  17. Attributes for BMB or Pieces on BMB
  18. Cache Optimization & Boom Area
  19. Change Boom Buttons Dynamically
  20. Blur Background & Tip
  21. Fade Views
  22. Structure of BMB
  23. Version History
Clone this wiki locally