标签:
效果如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UITableViewController 4 @property (strong, nonatomic) NSArray *arrColor; 5 6 - (id)initWithColorArray:(NSArray *)arrColor; 7 8 @end
ViewController.m
1 #import "ViewController.h" 2 3 @interface ViewController () 4 - (void)layoutUI; 5 - (void)highlightedSwitchDidPush; 6 @end 7 8 @implementation ViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 13 [self layoutUI]; 14 } 15 16 - (void)didReceiveMemoryWarning { 17 [super didReceiveMemoryWarning]; 18 // Dispose of any resources that can be recreated. 19 } 20 21 - (id)initWithColorArray:(NSArray *)arrColor { 22 if (self = [super initWithStyle:UITableViewStyleGrouped]) { 23 self.navigationItem.prompt = @"高亮显示屏幕中的文本"; 24 self.navigationItem.title = @"highlightedTextColor";; 25 26 _arrColor = arrColor; 27 } 28 return self; 29 } 30 31 - (void)layoutUI { 32 UIButton *btnHighlightedSwitch = [UIButton buttonWithType:UIButtonTypeCustom]; 33 btnHighlightedSwitch.frame = CGRectMake(0, 0, 160, 40); 34 CGPoint newPoint = self.view.center; 35 newPoint.y = self.view.frame.size.height - 150; 36 btnHighlightedSwitch.center = newPoint; 37 btnHighlightedSwitch.backgroundColor = [UIColor colorWithRed:0.940 green:1.000 blue:0.709 alpha:1.000]; 38 btnHighlightedSwitch.layer.masksToBounds = YES; 39 btnHighlightedSwitch.layer.cornerRadius = 10.0; 40 btnHighlightedSwitch.layer.borderColor = [UIColor colorWithRed:0.313 green:0.896 blue:1.000 alpha:1.000].CGColor; 41 btnHighlightedSwitch.layer.borderWidth = 2.0; 42 [btnHighlightedSwitch setTitle:@"highlighted切换" forState:UIControlStateNormal]; 43 [btnHighlightedSwitch setTitleColor:[UIColor colorWithRed:0.290 green:0.941 blue:1.000 alpha:1.000] forState:UIControlStateNormal]; 44 [btnHighlightedSwitch addTarget:self action:@selector(highlightedSwitchDidPush) forControlEvents:UIControlEventTouchUpInside]; 45 [self.view addSubview:btnHighlightedSwitch]; 46 } 47 48 - (void)highlightedSwitchDidPush { 49 NSInteger rowCount = [self.tableView numberOfRowsInSection:0]; 50 for (NSUInteger i=0; i<rowCount; i++) { 51 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0]; 52 UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 53 cell.textLabel.highlighted = !cell.textLabel.highlighted; 54 } 55 } 56 57 #pragma mark - UITableViewController相关方法重写 58 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 59 return @"UITableView列表"; 60 } 61 62 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 63 return 1; 64 } 65 66 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 67 return [_arrColor count]; 68 } 69 70 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 71 static NSString *cellIdentifier = @"cell"; 72 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 73 if (!cell) { 74 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 75 } 76 cell.textLabel.text = _arrColor[indexPath.row]; 77 cell.textLabel.highlightedTextColor = [UIColor performSelector:NSSelectorFromString(cell.textLabel.text)]; 78 return cell; 79 } 80 81 - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { 82 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 83 NSLog(@"%@选择前时是否高亮?%@", cell.textLabel.text, cell.textLabel.highlighted ? @"是" : @"否"); 84 return indexPath; 85 } 86 87 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 88 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 89 NSLog(@"%@已选择时是否高亮?%@", cell.textLabel.text, cell.textLabel.highlighted ? @"是" : @"否"); 90 } 91 92 @end
AppDelegate.h
1 #import <UIKit/UIKit.h> 2 3 @interface AppDelegate : UIResponder <UIApplicationDelegate> 4 @property (strong, nonatomic) UIWindow *window; 5 @property (strong, nonatomic) UINavigationController *navigationController; 6 7 @end
AppDelegate.m
1 #import "AppDelegate.h" 2 #import "ViewController.h" 3 4 @interface AppDelegate () 5 @end 6 7 @implementation AppDelegate 8 9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 10 _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 11 ViewController *viewController = [[ViewController alloc] initWithColorArray:@[@"redColor", @"greenColor", @"blueColor"]]; 12 _navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; 13 _window.rootViewController = _navigationController; 14 [_window addSubview:_navigationController.view]; 15 [_window makeKeyAndVisible]; 16 return YES; 17 } 18 19 - (void)applicationWillResignActive:(UIApplication *)application { 20 } 21 22 - (void)applicationDidEnterBackground:(UIApplication *)application { 23 } 24 25 - (void)applicationWillEnterForeground:(UIApplication *)application { 26 } 27 28 - (void)applicationDidBecomeActive:(UIApplication *)application { 29 } 30 31 - (void)applicationWillTerminate:(UIApplication *)application { 32 } 33 34 @end
标签:
原文地址:http://www.cnblogs.com/huangjianwu/p/4576625.html