想知道这是否可行...为了简单起见,我们假设我有一个使用导航控制器的应用程序,该应用程序有两个部分"新闻"和"体育"。
我在导航控制器中设置了一个自定义字体,它同时影响了新闻和体育游戏。有没有办法覆盖初始字体设置并使用单独的字体只为体育标题?因此,我有效地使用一种字体作为新闻标题,另一种字体用于体育标题。
非常感谢。
答案 0 :(得分:0)
试试这个。它的工作在Obj c。
for (UIView *view in self.navigationItem.titleView.subviews) {
[view removeFromSuperview];
}
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 200, 44)];
self.navigationItem.titleView.backgroundColor = [UIColor redColor];
self.navigationItem.titleView = textView;
NSString *newsTitle = @"News Title";
NSString *sportTtle = @"Sport Title";
NSString *title = [NSString stringWithFormat:@"%@ %@", newsTitle,sportTtle];
textView.text = title;
UIColor *color = [UIColor redColor];
UIFont *font = [UIFont fontWithName:@"Arial" size:20.0];
NSDictionary *attrs = @{NSForegroundColorAttributeName : color,NSFontAttributeName:font};//
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithAttributedString:textView.attributedText];
[attrStr addAttributes:attrs range:[textView.text rangeOfString:sportTtle]];
textView.attributedText = attrStr;
Swift Code:
func customizeNavigationTitle(){
let textView :UITextView = UITextView();
textView.frame = CGRectMake(0, 0, 200, 44)
self.navigationItem.titleView = textView;
let newsTitle:NSString = "News Title"
let sportTtle:NSString = "Sport Title"
var title = String(format: "%@%@", newsTitle,sportTtle)
textView.text = title;
var color:UIColor = UIColor.redColor();
var font:UIFont = UIFont(name: "Arial", size: 16)!
//Helvetica Neue
var attrs:AnyObject = [ NSForegroundColorAttributeName:color ,NSFontAttributeName: font]
var attrStr = NSMutableAttributedString(string:title, attributes:attrs as! [String : AnyObject]) as! NSMutableAttributedString;
var range = textView.text.rangeOfString(sportTtle as String)!
var attrSports = [ NSForegroundColorAttributeName:color ,NSFontAttributeName: font]
var textRange = NSRange(location:newsTitle.length , length: sportTtle.length)
attrStr.addAttribute(NSForegroundColorAttributeName, value: UIColor .greenColor(), range: textRange)
textView.attributedText = attrStr;
}
答案 1 :(得分:0)
将它放在viewDidLoad()
中titleLabel = UILabel(frame: CGRectMake(0, 0, 200, 44))
titleLabel.text = "Discover"
titleLabel.font = UIFont(name: "SFUIDisplay-Light", size: 21)
titleLabel.textColor = UIColor.whiteColor()
titleLabel.textAlignment = NSTextAlignment.Center
self.navigationItem.titleView = titleLabel