我有一个UINavigationBar
标题,我想将所有字母大写,这样“单词”就变成了“单词”。
我想对这个导航栏中的按钮做同样的事情。
我想知道是否有类似 CSStext-transform:uppercase;
属性的东西可以改变单词的视觉外观,但是一旦检索到它的值,它仍然返回它的原始值。
先感谢您
我有一个UINavigationBar
标题,我想将所有字母大写,这样“单词”就变成了“单词”。
我想对这个导航栏中的按钮做同样的事情。
我想知道是否有类似 CSStext-transform:uppercase;
属性的东西可以改变单词的视觉外观,但是一旦检索到它的值,它仍然返回它的原始值。
先感谢您
这实际上比你想象的要简单。您可以使用 NSStringuppercaseString
来实现这一点。
[myLabel setText:[myString uppercaseString]];
或者,更适合您的需求:
for (UIBarButtonItem *button in self.navigationItem.rightBarButtonItems) { //Your nag bar
[button setTitle:[[button title] uppercaseString]];
}
编辑:字符串不改变的例子。
NSString *myString = @"hello";
NSString *mySecondString = @"world!";
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:[myString uppercaseString] style:UIBarButtonItemStyleBordered target:self action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:[mySecondString uppercaseString] style:UIBarButtonItemStyleBordered target:self action:nil];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:item1,item2, nil]];
NSLog(@"%@ | %@",myString,mySecondString);//this will output the same as when it was originally created.
As using [myString uppercaseString]
has the downside of returning an uppercase string (well, it is not actually a downside ..) and not the original string, one option would be to set a UILabel with an all-caps font (Copperplate is just an example of an all-caps font, you will likely find a better one):
int height = navigationController.navigationBar.frame.size.height;
int width = navigationController.navigationBar.frame.size.width;
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, height)];
navLabel.backgroundColor = [UIColor clearColor];
navLabel.textColor = [UIColor whiteColor];
navLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
navLabel.font = [UIFont fontWithName:@"Copperplate" size:18];
navLabel.textAlignment = UITextAlignmentCenter;
self.navigationItem.titleView = navLabel;
有 !
NSString *myLowercaseString = @"a lowercase string";
NSString *myUppercaseString = [myLowercaseString uppercaseString];
如果你NSLog (@"%@", myUppercaseString);
,你会得到以下输出:
小写字符串
然后,您可以将按钮或导航栏的文本属性设置为您的字符串。