iOS Recipes - Matt Drance [13]
RoundedView/Classes/RoundedViewViewController.m
// Defining the Textured colors from UIImages
thickColor = [UIColor colorWithPatternImage:
[UIImage imageNamed:@"thickColorGradient.png"]];
UIColor *grayGradient = [UIColor colorWithPatternImage:
[UIImage imageNamed:@"grayGradient.png"]];
UIColor *steelColor = [UIColor colorWithPatternImage:
[UIImage imageNamed:@"simpleSteel.png"]];
UIColor *steelTexture = [UIColor colorWithPatternImage:
[UIImage imageNamed:@"steelTexture.png"]];
UIColor *woodTexture = [UIColor colorWithPatternImage:
[UIImage imageNamed:@"woodTexture.png"]];
CGRect buttonFrame = CGRectMake(60, 60, 200,80);
UIButton *roundButton = [[UIButton alloc] initWithFrame:buttonFrame];
roundButton.layer.borderWidth = 8;
roundButton.layer.borderColor = thickColor.CGColor;
roundButton.layer.backgroundColor = grayGradient.CGColor;
roundButton.layer.cornerRadius = roundButton.bounds.size.height/4;
[self.view addSubview:roundButton];
[roundButton addTarget:self action:@selector(buttonPressed:)
forControlEvents:UIControlEventTouchDown];
[roundButton addTarget:self action:@selector(buttonReleased:)
forControlEvents:UIControlEventTouchUpInside |
UIControlEventTouchUpOutside];
Here we are using a UIView with a UILabel subview. The view’s layer is manipulated like the button earlier to give an interesting background for the label.
RoundedView/Classes/RoundedViewViewController.m
UILabel *labelA = [self centeredLabel:buttonFrame label:@"Colorful"];
labelA.font = [UIFont fontWithName:@"MarkerFelt-Thin" size:36];
labelA.textColor = thickColor;
[roundButton addSubview:labelA];
CGRect viewFrame = CGRectMake(30, 210, 260, 50);
UIView *steelView = [[UIView alloc] initWithFrame:viewFrame];
steelView.layer.borderWidth = 5;
steelView.layer.borderColor = steelColor.CGColor;
steelView.layer.backgroundColor = steelTexture.CGColor;
steelView.layer.cornerRadius = steelView.bounds.size.height/4;
[self.view addSubview:steelView];
UILabel *labelB = [self centeredLabel:viewFrame label:@"Brushed Steel"];
labelB.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:28];
labelB.textColor = steelColor;
[steelView addSubview:labelB];
We could go even further and modify the same properties directly on the UILabel’s layer to achieve the same effect.
RoundedView/Classes/RoundedViewViewController.m
CGRect labelFrame = CGRectMake(10, 340, 300, 40);
UILabel *label = [self centeredLabel:labelFrame
label:@"A Much Longer Label"];
label.frame = labelFrame;
label.font = [UIFont fontWithName:@"Thonburi-Bold" size:24];
label.textColor = steelColor;
label.shadowColor = [UIColor blackColor];
label.layer.borderWidth = 4;
label.layer.borderColor = steelColor.CGColor;
label.layer.backgroundColor = woodTexture.CGColor;
label.layer.cornerRadius = label.frame.size.height/2;
[self.view addSubview:label];
Other properties are exposed by the CALayer class that are not available to the UIView class, so it’s well worth checking out the iOS documentation to see what other interesting effects you can achieve.
Recipe 6 Put Together a Reusable Web View
Problem
Some of the most elegant and customized native apps on the market still rely on web content now and then, if only to open a URL without punting to Safari. UIWebView is an elegant class that’s easy to use, but there’s a decent amount of support code that goes into displaying even a single web page.
Solution
We can save ourselves time and effort over multiple projects by making a basic web view that’s displayable either modally or as part of a navigation stack. The controller takes a URL from the calling code and automatically loads the content when the view is loaded. It displays an activity indicator view while the page loads, and it performs a smooth transition once the content is ready to be displayed. We fire up this controller, display it with just a few lines of code, then get back to more important business.