### Initialize HMSegmentedControl with Combined Text and Images (Objective-C) Source: https://context7.com/heshammegid/hmsegmentedcontrol/llms.txt Initializes an HMSegmentedControl with both images and text titles for each segment, offering greater flexibility in UI design. This example demonstrates setting image position, spacing, segment width style, and selection style for a visually rich control. ```objective-c NSArray *images = @[ [UIImage imageNamed:@"star"], [UIImage imageNamed:@"heart"], [UIImage imageNamed:@"bell"], [UIImage imageNamed:@"message"] ]; NSArray *selectedImages = @[ [UIImage imageNamed:@"star-filled"], [UIImage imageNamed:@"heart-filled"], [UIImage imageNamed:@"bell-filled"], [UIImage imageNamed:@"message-filled"] ]; NSArray *titles = @[@"Favorites", @"Likes", @"Alerts", @"Messages"]; HMSegmentedControl *segmentedControl = [[HMSegmentedControl alloc] initWithSectionImages:images sectionSelectedImages:selectedImages titlesForSections:titles]; segmentedControl.frame = CGRectMake(0, 100, self.view.frame.size.width, 60); segmentedControl.imagePosition = HMSegmentedControlImagePositionAboveText; segmentedControl.textImageSpacing = 5.0f; segmentedControl.segmentWidthStyle = HMSegmentedControlSegmentWidthStyleDynamic; segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe; segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationBottom; [self.view addSubview:segmentedControl]; ``` -------------------------------- ### Install HMSegmentedControl using CocoaPods Source: https://github.com/heshammegid/hmsegmentedcontrol/blob/master/README.md This snippet shows how to install the HMSegmentedControl library using CocoaPods, a dependency manager for Swift and Objective-C projects. Ensure you have CocoaPods installed and configured in your project. ```ruby pod 'HMSegmentedControl' ``` -------------------------------- ### Custom Title Styling with Attributed Strings in HMSegmentedControl (Objective-C) Source: https://context7.com/heshammegid/hmsegmentedcontrol/llms.txt This example shows how to use a formatter block with HMSegmentedControl to apply custom styling to segment titles using attributed strings. It allows dynamic changes in text color, font, alignment, and even appending emojis based on selection state. This enhances the visual appeal and information conveyed by each segment. ```objective-c HMSegmentedControl *segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"Premium", @"Standard", @"Basic"]]; segmentedControl.frame = CGRectMake(20, 100, self.view.frame.size.width - 40, 50); [segmentedControl setTitleFormatter:^NSAttributedString *( HMSegmentedControl *control, NSString *title, NSUInteger index, BOOL selected) { UIColor *textColor = selected ? [UIColor systemBlueColor] : [UIColor grayColor]; UIFont *font = selected ? [UIFont boldSystemFontOfSize:18] : [UIFont systemFontOfSize:16]; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.alignment = NSTextAlignmentCenter; NSDictionary *attributes = @{ NSForegroundColorAttributeName: textColor, NSFontAttributeName: font, NSParagraphStyleAttributeName: paragraphStyle, NSKernAttributeName: @(1.5) }; // Add emoji for Premium tier NSString *displayTitle = [title isEqualToString:@"Premium"] ? [NSString stringWithFormat:@"⭐ %@", title] : title; return [[NSAttributedString alloc] initWithString:displayTitle attributes:attributes]; }]; segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe; segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationBottom; [self.view addSubview:segmentedControl]; ``` -------------------------------- ### Create and Configure HMSegmentedControl in Swift Source: https://github.com/heshammegid/hmsegmentedcontrol/blob/master/README.md This Swift code demonstrates the basic creation and configuration of an HMSegmentedControl. It initializes the control with section titles, sets its frame, adds a target action for value changes, and adds it to the view hierarchy. Dependencies include the HMSegmentedControl library. ```swift let segmentedControl = HMSegmentedControl(sectionTitles: [ "Trending", "News", "Library" ]) segmentedControl.frame = CGRect(x: 0, y: 0, width: 100, height: 40) segmentedControl.addTarget(self, action: #selector(segmentedControlChangedValue(segmentedControl:)), for: .valueChanged) view.addSubview(segmentedControl) ``` -------------------------------- ### Initialize HMSegmentedControl with Images (Objective-C) Source: https://context7.com/heshammegid/hmsegmentedcontrol/llms.txt Initializes an HMSegmentedControl using separate arrays for normal and selected state images. This allows for distinct visual feedback when a segment is tapped. The control is added to the view, and a target-action is set up to respond to changes. ```objective-c #import "HMSegmentedControl.h" @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSArray *images = @[ [UIImage imageNamed:@"home"], [UIImage imageNamed:@"search"], [UIImage imageNamed:@"profile"], [UIImage imageNamed:@"settings"] ]; NSArray *selectedImages = @[ [UIImage imageNamed:@"home-selected"], [UIImage imageNamed:@"search-selected"], [UIImage imageNamed:@"profile-selected"], [UIImage imageNamed:@"settings-selected"] ]; HMSegmentedControl *segmentedControl = [[HMSegmentedControl alloc] initWithSectionImages:images sectionSelectedImages:selectedImages]; segmentedControl.frame = CGRectMake(0, 100, self.view.frame.size.width, 50); segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationBottom; segmentedControl.selectionIndicatorHeight = 3.0f; [segmentedControl addTarget:self action:@selector(segmentedControlChanged:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:segmentedControl]; } - (void)segmentedControlChanged:(HMSegmentedControl *)control { NSLog(@"Selected segment: %lu", (unsigned long)control.selectedSegmentIndex); } @end ``` -------------------------------- ### Initialize HMSegmentedControl with Text Titles (Swift) Source: https://context7.com/heshammegid/hmsegmentedcontrol/llms.txt Initializes an HMSegmentedControl with an array of string titles for its segments. This method is suitable for creating basic text-based segmented controls. It requires importing UIKit and adding the control to the view hierarchy, with a target-action mechanism to handle value changes. ```swift import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let segmentedControl = HMSegmentedControl(sectionTitles: [ "Trending", "News", "Library" ]) segmentedControl.frame = CGRect(x: 0, y: 100, width: view.frame.width, height: 40) segmentedControl.addTarget(self, action: #selector(segmentedControlChangedValue(_:)), for: .valueChanged) view.addSubview(segmentedControl) } @objc func segmentedControlChangedValue(_ control: HMSegmentedControl) { print("Selected index: (control.selectedSegmentIndex)") } } ``` -------------------------------- ### Block-Based Selection Callback in Objective-C Source: https://context7.com/heshammegid/hmsegmentedcontrol/llms.txt Implements segment selection using a block-based callback instead of the traditional target-action pattern. This allows for direct handling of segment changes and updating UI elements accordingly. Dependencies include the HMSegmentedControl class. ```objective-c HMSegmentedControl *segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@["Daily", "Weekly", "Monthly", "Yearly"]]; segmentedControl.frame = CGRectMake(20, 100, self.view.frame.size.width - 40, 44); segmentedControl.selectedSegmentIndex = 0; __weak typeof(self) weakSelf = self; [segmentedControl setIndexChangeBlock:^(NSUInteger index) { NSLog(@"Segment changed to index: %lu", (unsigned long)index); // Update content based on selection switch (index) { case 0: [weakSelf loadDailyStats]; break; case 1: [weakSelf loadWeeklyStats]; break; case 2: [weakSelf loadMonthlyStats]; break; case 3: [weakSelf loadYearlyStats]; break; } }]; [self.view addSubview:segmentedControl]; // When user taps "Weekly": Output: Segment changed to index: 1 ``` -------------------------------- ### Synchronize HMSegmentedControl with UIScrollView (Objective-C) Source: https://context7.com/heshammegid/hmsegmentedcontrol/llms.txt This code demonstrates how to synchronize an HMSegmentedControl with a paging UIScrollView for intuitive tab-like navigation. When the user scrolls the content, the selected segment updates accordingly, and vice-versa. This involves implementing the UIScrollViewDelegate to handle scroll events and updating the segmented control's selection. ```objective-c @interface ViewController () @property (nonatomic, strong) HMSegmentedControl *segmentedControl; @property (nonatomic, strong) UIScrollView *contentScrollView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; CGFloat width = self.view.frame.size.width; // Create segmented control self.segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"Worldwide", @"Local", @"Headlines"]]; self.segmentedControl.frame = CGRectMake(0, 100, width, 44); self.segmentedControl.selectedSegmentIndex = 0; self.segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe; self.segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationBottom; // Handle segment selection __weak typeof(self) weakSelf = self; [self.segmentedControl setIndexChangeBlock:^(NSUInteger index) { CGRect frame = CGRectMake(width * index, 0, width, 300); [weakSelf.contentScrollView scrollRectToVisible:frame animated:YES]; }]; [self.view addSubview:self.segmentedControl]; // Create paging scroll view self.contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 150, width, 300)]; self.contentScrollView.pagingEnabled = YES; self.contentScrollView.showsHorizontalScrollIndicator = NO; self.contentScrollView.contentSize = CGSizeMake(width * 3, 300); self.contentScrollView.delegate = self; [self.view addSubview:self.contentScrollView]; // Add content pages for (int i = 0; i < 3; i++) { UIView *page = [[UIView alloc] initWithFrame:CGRectMake(width * i, 0, width, 300)]; page.backgroundColor = [UIColor colorWithHue:(i * 0.2) saturation:0.5 brightness:0.9 alpha:1.0]; [self.contentScrollView addSubview:page]; } } #pragma mark - UIScrollViewDelegate - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { CGFloat pageWidth = scrollView.frame.size.width; NSInteger page = scrollView.contentOffset.x / pageWidth; // Update segment selection based on scroll position [self.segmentedControl setSelectedSegmentIndex:page animated:YES]; } @end ``` -------------------------------- ### Box Selection Style in Objective-C Source: https://context7.com/heshammegid/hmsegmentedcontrol/llms.txt Configures a segmented control with a box-style selection indicator that fully covers the selected segment. It sets text attributes for white text on a colored background and uses `HMSegmentedControlSelectionStyleBox` for the selection. ```objective-c HMSegmentedControl *segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@["One", "Two", "Three", "Four", "Five"]]; segmentedControl.frame = CGRectMake(0, 100, self.view.frame.size.width, 50); segmentedControl.backgroundColor = [UIColor colorWithRed:0.1 green:0.4 blue:0.8 alpha:1.0]; segmentedControl.titleTextAttributes = @{ NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: [UIFont systemFontOfSize:17] }; segmentedControl.selectedTitleTextAttributes = @{ NSForegroundColorAttributeName: [UIColor colorWithRed:0.1 green:0.4 blue:0.8 alpha:1.0] }; segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleBox; segmentedControl.selectionIndicatorBoxColor = [UIColor whiteColor]; segmentedControl.selectionIndicatorBoxOpacity = 1.0; segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationNone; segmentedControl.selectedSegmentIndex = 0; [self.view addSubview:segmentedControl]; ``` -------------------------------- ### Arrow Selection Indicator in Objective-C Source: https://context7.com/heshammegid/hmsegmentedcontrol/llms.txt Configures an HMSegmentedControl in Objective-C to use an arrow-style selection indicator. This involves setting the selectionStyle to HMSegmentedControlSelectionStyleArrow and specifying the indicator's location, height, and color. Text attributes for both normal and selected states are also customizable. ```objective-c HMSegmentedControl *segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@["Option A", "Option B", "Option C"]]; segmentedControl.frame = CGRectMake(20, 100, self.view.frame.size.width - 40, 44); segmentedControl.backgroundColor = [UIColor whiteColor]; // Configure arrow style segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleArrow; segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationBottom; segmentedControl.selectionIndicatorHeight = 10.0f; segmentedControl.selectionIndicatorColor = [UIColor systemRedColor]; segmentedControl.titleTextAttributes = @{ NSForegroundColorAttributeName: [UIColor darkGrayColor], NSFontAttributeName: [UIFont systemFontOfSize:15] }; segmentedControl.selectedTitleTextAttributes = @{ NSForegroundColorAttributeName: [UIColor systemRedColor] }; [self.view addSubview:segmentedControl]; ``` -------------------------------- ### Custom Styling with Text Attributes in Objective-C Source: https://context7.com/heshammegid/hmsegmentedcontrol/llms.txt Applies custom fonts, colors, and text attributes for both normal and selected states of the segmented control. It uses NSForegroundColorAttributeName and NSFontAttributeName for styling, and configures selection indicator properties. ```objective-c HMSegmentedControl *segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@["Home", "Browse", "Search", "Profile"]]; segmentedControl.frame = CGRectMake(0, 100, self.view.frame.size.width, 50); segmentedControl.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.15 alpha:1.0]; // Style for unselected segments segmentedControl.titleTextAttributes = @{ NSForegroundColorAttributeName: [UIColor colorWithWhite:0.7 alpha:1.0], NSFontAttributeName: [UIFont systemFontOfSize:16 weight:UIFontWeightMedium], NSKernAttributeName: @(1.2) }; // Style for selected segment segmentedControl.selectedTitleTextAttributes = @{ NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: [UIFont systemFontOfSize:16 weight:UIFontWeightBold], NSKernAttributeName: @(1.2) }; segmentedControl.selectionIndicatorColor = [UIColor colorWithRed:0.2 green:0.6 blue:1.0 alpha:1.0]; segmentedControl.selectionIndicatorHeight = 3.0f; segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe; segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationBottom; [self.view addSubview:segmentedControl]; ``` -------------------------------- ### Horizontal Scrolling with Dynamic Widths in Objective-C Source: https://context7.com/heshammegid/hmsegmentedcontrol/llms.txt Enables horizontal scrolling for a segmented control with dynamically sized segments based on their content width. It uses `HMSegmentedControlSegmentWidthStyleDynamic` and `userDraggable = YES` to allow scrolling and swiping. ```objective-c HMSegmentedControl *segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[ "All", "Technology", "Business", "Entertainment", "Sports", "Science", "Health", "Politics" ]]; segmentedControl.frame = CGRectMake(0, 100, self.view.frame.size.width, 40); segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth; // Enable dynamic width sizing segmentedControl.segmentWidthStyle = HMSegmentedControlSegmentWidthStyleDynamic; segmentedControl.segmentEdgeInset = UIEdgeInsetsMake(0, 12, 0, 12); // Enable scrolling segmentedControl.userDraggable = YES; // Style the selection indicator segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe; segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationBottom; segmentedControl.selectionIndicatorHeight = 3.0f; segmentedControl.selectionIndicatorColor = [UIColor colorWithRed:0.95 green:0.26 blue:0.21 alpha:1.0]; [self.view addSubview:segmentedControl]; ``` -------------------------------- ### Add Vertical Dividers to HMSegmentedControl (Objective-C) Source: https://context7.com/heshammegid/hmsegmentedcontrol/llms.txt This code snippet demonstrates how to add vertical divider lines between segments in an HMSegmentedControl for improved visual separation. It configures the divider's appearance and width. No external dependencies are required beyond HMSegmentedControl. ```objective-c HMSegmentedControl *segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"Morning", @"Afternoon", @"Evening", @"Night"]]; segmentedControl.frame = CGRectMake(0, 100, self.view.frame.size.width, 44); segmentedControl.backgroundColor = [UIColor whiteColor]; // Enable vertical dividers segmentedControl.verticalDividerEnabled = YES; segmentedControl.verticalDividerColor = [UIColor colorWithWhite:0.85 alpha:1.0]; segmentedControl.verticalDividerWidth = 1.0f; segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe; segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationBottom; segmentedControl.selectionIndicatorHeight = 2.0f; // Add border segmentedControl.borderType = HMSegmentedControlBorderTypeBottom; segmentedControl.borderColor = [UIColor colorWithWhite:0.9 alpha:1.0]; segmentedControl.borderWidth = 1.0f; [self.view addSubview:segmentedControl]; ``` -------------------------------- ### Programmatic Selection Changes in Swift Source: https://context7.com/heshammegid/hmsegmentedcontrol/llms.txt Demonstrates how to programmatically change the selected segment of an HMSegmentedControl in Swift, with options for animation. This control is initialized with section titles and added as a subview. It handles selection changes via methods like setSelectedSegmentIndex, which can trigger valueChanged events. ```swift import UIKit class StatsViewController: UIViewController { var segmentedControl: HMSegmentedControl! override func viewDidLoad() { super.viewDidLoad() segmentedControl = HMSegmentedControl(sectionTitles: [ "Today", "Week", "Month", "Year" ]) segmentedControl.frame = CGRect(x: 0, y: 100, width: view.frame.width, height: 44) segmentedControl.selectedSegmentIndex = 0 view.addSubview(segmentedControl) } func showWeeklyStats() { // Change to "Week" segment with animation segmentedControl.setSelectedSegmentIndex(1, animated: true) // This will trigger the valueChanged event and any indexChangeBlock } func clearSelection() { // Remove selection entirely segmentedControl.setSelectedSegmentIndex(HMSegmentedControlNoSegment, animated: false) // No segment will be highlighted } func handleDeepLink(period: String) { let index: UInt switch period { case "today": index = 0 case "week": index = 1 case "month": index = 2 case "year": index = 3 default: return } // Change segment without animation for instant response segmentedControl.setSelectedSegmentIndex(index, animated: false) } } ``` -------------------------------- ### Disable Animation and Touch Interactions in Objective-C Source: https://context7.com/heshammegid/hmsegmentedcontrol/llms.txt Shows how to disable user interaction and animation effects on an HMSegmentedControl using Objective-C. Key properties include `shouldAnimateUserSelection` to control animation on user taps, `touchEnabled` to disable all touch interactions, and `userDraggable` to prevent scrolling while allowing taps. ```objective-c HMSegmentedControl *segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@["Read Only", "Mode", "Active"]]; segmentedControl.frame = CGRectMake(0, 100, self.view.frame.size.width, 44); segmentedControl.selectedSegmentIndex = 1; // Disable animation on user selection segmentedControl.shouldAnimateUserSelection = NO; // Programmatic updates can still be animated [segmentedControl setSelectedSegmentIndex:2 animated:YES]; // To completely disable user interaction segmentedControl.touchEnabled = NO; // To disable scrolling but allow tapping segmentedControl.userDraggable = NO; [self.view addSubview:segmentedControl]; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.