Xcode在IOS5之後,提供一個特殊功能來顯示文字陰影。這個功能可以在有文字顯示需求的地方來使用,例如SegmentController/NavigationBar/TabBar 之類的,只要有setTitleTextAttributes這個function可以呼叫來加入屬性,就可以使用。實驗顯示的效果並不美觀,不知在何種應用需要這個功能。
官方文件
http://developer.apple.com/library/ios/#documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html
以下的實驗例,改自UISegmentedControl的使用(三)。
1. 加入兩個Slider到storyboard,並外加兩個Label來註解。
2. 在mainViewController.h加入Slider的註冊,並外加Toolbar及BarItem的元件。
#import <UIKit/UIKit.h>
@interface mainViewController : UIViewController
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentSelector;
@property (nonatomic) UISegmentedControl *selector1;
@property (nonatomic) UISegmentedControl *selector2;
@property (nonatomic) UISegmentedControl *selector3;
@property (weak, nonatomic) IBOutlet UISlider *shadowXSlider;
@property (weak, nonatomic) IBOutlet UISlider *shadowYSlider;
@property (nonatomic) UIToolbar *myToolBar;
@property (nonatomic) UIBarButtonItem *myBarItem1;
@property (nonatomic) UIBarButtonItem *myBarItem2;
@property (nonatomic) UIBarButtonItem *myBarItem3;
@property (nonatomic) UIBarButtonItem *myBarItem4;
@property (nonatomic) UIBarButtonItem *myBarItem5;@end
3.在mainViewController.m加入相關程式碼
#import "mainViewController.h"
@interface mainViewController ()
@end
@implementation mainViewController
@synthesize segmentSelector;
@synthesize selector1, selector2, selector3;
@synthesize shadowXSlider, shadowYSlider;
@synthesize myToolBar;
@synthesize myBarItem1, myBarItem2, myBarItem3, myBarItem4, myBarItem5;- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setupMyToolBar];
[self.view addSubview:myToolBar]; }
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)selectOtherSegment:(id)sender {
NSNumber *value = [NSNumber numberWithInt:[sender selectedSegmentIndex]];
switch ([sender selectedSegmentIndex]) {
case 0:
[self setSelector1];
break;
case 1:
[self setSelector2];
break;
case 2:
[self setSelector3];
break;
default:
[self setSelector1];
break;
}
}
- (void) setSelector1
{
[self removeSubViewByLabelClass];
NSArray *itemArray =[NSArray arrayWithObjects:@"Green Apple", @"Orange", @"Pear",@"Red Apple", @"Strawberry",nil];
//使用陣列來建立UISegmentedControl
selector1 = [[UISegmentedControl alloc] initWithItems:itemArray];
//設定外觀大小與初始選項
selector1.segmentedControlStyle = UISegmentedControlStyleBar;
selector1.frame = CGRectMake(20.0, 100.0, 660.0, 44.0);
selector1.selectedSegmentIndex = 0;
UIImage *image1 = [UIImage imageNamed:@"Orange.gif"];
//[[selector1 appearance] setBackgroundImage:image1 forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[selector1 setBackgroundImage:image1 forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
UIImage *segmentedControl = [UIImage imageNamed:@"pear.gif"];
[selector1 setDividerImage:segmentedControl forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
UIFont *font = [UIFont boldSystemFontOfSize:16];//改變segment的字體大小
NSDictionary *attribute = [NSDictionary dictionaryWithObject:font forKey:UITextAttributeFont];
[selector1 setTitleTextAttributes:attribute forState:UIControlStateNormal];
//selector1.momentary = YES; // 不顯示被選的狀態
selector1.tag = 1;
//設定所觸發的事件條件與對應事件
[selector1 addTarget:self action:@selector(chooseOne:) forControlEvents:UIControlEventValueChanged];
//加入畫面中並釋放記憶體
[self.view addSubview:selector1];
}
- (void) setSelector2
{
[self removeSubViewByLabelClass];
NSArray *itemArray =[NSArray arrayWithObjects:@"Bluehound", @"Doggie", @"Pointy",@"PurpGuy",@"RedDog",@"Woof", nil];
//使用陣列來建立UISegmentedControl
selector2 = [[UISegmentedControl alloc] initWithItems:itemArray];
//設定外觀大小與初始選項
selector2.segmentedControlStyle = UISegmentedControlStyleBar;
selector2.frame = CGRectMake(20.0, 200.0, 600.0, 44.0);
selector2.selectedSegmentIndex = 0;
selector2.tag = 2;
//[UIColor clearColor], UITextAttributeTextShadowColor,
// [selector2 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
// [UIColor magentaColor],UITextAttributeTextColor,
// [UIColor blackColor], UITextAttributeTextShadowColor,
// [NSValue valueWithUIOffset:UIOffsetMake(0, -5)], UITextAttributeTextShadowOffset,
// [UIFont fontWithName:@"Courier-Oblique" size:22.0], UITextAttributeFont, nil] forState:UIControlStateNormal];
//
//UIFont *font = [UIFont boldSystemFontOfSize:16];//改變segment的字體大小
NSDictionary *attribute = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor magentaColor], UITextAttributeTextColor,
[UIColor blackColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(shadowXSlider.value, shadowYSlider.value)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:@"Courier-Oblique" size:22.0], UITextAttributeFont,
nil];
[selector2 setTitleTextAttributes:attribute forState:UIControlStateNormal];
//設定所觸發的事件條件與對應事件
[selector2 addTarget:self action:@selector(chooseOne:) forControlEvents:UIControlEventValueChanged];
//加入畫面中並釋放記憶體
[self.view addSubview:selector2];
}
- (void) setSelector3
{
[self removeSubViewByLabelClass];
NSArray *itemArray =[NSArray arrayWithObjects:@"African Daisy", @"Dandelion", @"Ixia", @"Spiked",@"Sunflower",nil];
//使用陣列來建立UISegmentedControl
selector3 = [[UISegmentedControl alloc] initWithItems:itemArray];
//設定外觀大小與初始選項
selector3.segmentedControlStyle = UISegmentedControlStylePlain; //UISegmentedControlStyleBar;
selector3.frame = CGRectMake(20.0, 300.0, 550.0, 44.0);
selector3.selectedSegmentIndex = 0;
selector3.tag = 3;
//設定所觸發的事件條件與對應事件
[selector3 addTarget:self action:@selector(chooseOne:) forControlEvents:UIControlEventValueChanged];
//加入畫面中並釋放記憶體
[self.view addSubview:selector3];
}
- (void)chooseOne:(id)sender {
[self removeSubViewByTag:101];
//NSLog(@"%@", [sender titleForSegmentAtIndex:[sender selectedSegmentIndex]]);
UIImageView *a1 = [[UIImageView alloc]initWithFrame:CGRectMake(100, 500, 200,200)];
[a1 setImage: [UIImage imageNamed:[NSString stringWithFormat:@"%@.gif",[sender titleForSegmentAtIndex:[sender selectedSegmentIndex]]]]];
//a1.alpha = 0.3f;
a1.tag = 101;
[self.view addSubview:a1];
[self.view sendSubviewToBack:a1];
}
- (void) removeSubViewByTag:(int)tag
{
for(UIView *subview in [self.view subviews]) {
if (subview.tag == tag)
[subview removeFromSuperview];
}
}
- (void) removeSubViewByLabelClass
{
for(UIView *subview in [self.view subviews]) {
if([subview isKindOfClass:[UISegmentedControl class]]) {
if (subview.tag != 100)
[subview removeFromSuperview];
} else {
// Do nothing - not a UIButton or subclass instance
}
}
}
-(void)setupMyToolBar
{
myToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 684, 1024, 64.0)];
NSDictionary *attribute1 = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor magentaColor], UITextAttributeTextColor,
[UIColor blackColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(-5, 5)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:@"Courier-Oblique" size:16.0], UITextAttributeFont,
nil];
NSDictionary *attribute2 = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor magentaColor], UITextAttributeTextColor,
[UIColor blueColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(10, 10)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:@"Courier-Oblique" size:22.0], UITextAttributeFont,
nil];
NSDictionary *attribute3 = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor redColor], UITextAttributeTextColor,
[UIColor blackColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(-10, -10)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:@"Courier-Oblique" size:22.0], UITextAttributeFont,
nil];
NSDictionary *attribute4 = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor magentaColor], UITextAttributeTextColor,
[UIColor greenColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(-15, 15)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:@"Courier" size:30.0], UITextAttributeFont,
nil];
NSDictionary *attribute5 = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blueColor], UITextAttributeTextColor,
[UIColor greenColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(10, 10)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:@"Courier" size:30.0], UITextAttributeFont,
nil];
//myBarItem1= [[UIBarButtonItem alloc] initWithCustomView:label];
myBarItem1= [[UIBarButtonItem alloc]initWithTitle:@"Test1" style:UIBarButtonItemStylePlain target:self action:@selector(showText1)];
myBarItem2= [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemFixedSpace) target:nil action:nil];
myBarItem3= [[UIBarButtonItem alloc]initWithTitle:@"Test2" style:UIBarButtonItemStyleBordered target:self action:@selector(showText2)];
myBarItem4= [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemFlexibleSpace) target:nil action:nil];
myBarItem5= [[UIBarButtonItem alloc]initWithTitle:@"Test3" style:UIBarButtonItemStyleDone target:self action:@selector(showText3)];
[myBarItem1 setTitleTextAttributes:attribute1 forState:UIControlStateNormal];
[myBarItem2 setTitleTextAttributes:attribute2 forState:UIControlStateNormal];
[myBarItem3 setTitleTextAttributes:attribute3 forState:UIControlStateNormal];
[myBarItem4 setTitleTextAttributes:attribute4 forState:UIControlStateNormal];
[myBarItem5 setTitleTextAttributes:attribute5 forState:UIControlStateNormal];
myBarItem2.width = 420;
NSArray *toolbarItems = [[NSArray alloc] initWithObjects:myBarItem1,myBarItem2,myBarItem3,myBarItem4,myBarItem5,nil];
myToolBar.items = toolbarItems;
}@end
4. 顯示結果,Toolbar上每個Item的字型及陰影都不同,而Segment2的陰影則可由兩個Slider來控制,只是不知道什麼樣的應用需要而已。