我有一些视图显示在导航控制器中。其中两个视图对导航栏的标题较长。如何在动态导航栏中调整标题大小
问题是,当标题太长以至于不适合时,某些字符会被截断并添加“...”。
有什么办法可以告诉导航栏自动重新调整标题文本的大小以适应?
我有一些视图显示在导航控制器中。其中两个视图对导航栏的标题较长。如何在动态导航栏中调整标题大小
问题是,当标题太长以至于不适合时,某些字符会被截断并添加“...”。
有什么办法可以告诉导航栏自动重新调整标题文本的大小以适应?
用于viewDidLoad中下面的代码。
目标C
self.title = @"Your TiTle Text";
UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 200, 40)];
tlabel.text=self.navigationItem.title;
tlabel.textColor=[UIColor whiteColor];
tlabel.font = [UIFont fontWithName:@"Helvetica-Bold" size: 30.0];
tlabel.backgroundColor =[UIColor clearColor];
tlabel.adjustsFontSizeToFitWidth=YES;
tlabel.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView=tlabel;
斯威夫特版本
self.title = "Your TiTle Text"
let tlabel = UILabel(frame: CGRectMake(0, 0, 200, 40))
tlabel.text = self.title
tlabel.textColor = UIColor.whiteColor()
tlabel.font = UIFont(name: "Helvetica-Bold", size: 30.0)
tlabel.backgroundColor = UIColor.clearColor()
tlabel.adjustsFontSizeToFitWidth = true
tlabel.textAlignment = .center;
self.navigationItem.titleView = tlabel
希望工程为周到,谢谢
感谢鲍勃,工作得很好! – PhoenixDev 2013-02-20 06:12:00
欢迎您! – jamil 2013-02-20 06:21:34
你需要定制具有的UILabel导航栏标题视图,并提供调整字体大小..
[self.navigationItem setTitleView:<"Include any UI View subclass">];
下面是斯威夫特的例子还允许多条线路。使用PureLayout可简化自动布局。
override func viewDidLoad() {
super.viewDidLoad()
configureTitleView()
}
func configureTitleView() {
let titleLabel = UILabel()
titleLabel.numberOfLines = 0
titleLabel.textAlignment = .Center
titleLabel.font = UIFont.boldSystemFontOfSize(17.0)
titleLabel.text = searchLoc.mapItem.name
navigationItem.titleView = titleLabel
titleLabel.autoPinEdgesToSuperviewMargins() // PureLayout method
titleLabel.adjustsFontSizeToFitWidth = true
}
以及使用例如:
不缝合工作,因为当titleLabel.autoPinEdgesToSuperviewMargins被调用时,titleLabel的超级视图是(仍然)为零,因此设置约束断言。 (xcode 7.3,IOS9) – HixField 2016-09-04 06:00:00
上述解决方案缝都不可靠地为我工作。 但是我发现了一个解决方案,它使用Swift 2中的不同元素提供答案,并且它非常优雅,因为每次更改标签时都不需要任何自定义代码,它只是在标题上使用属性观察器。
请注意,在我的情况下,我在导航栏的左侧有一个后退按钮,将文本置于屏幕中央,以解决此问题,我正在使用属性文本和tailIndent。所有评论在下面的代码/信息:
class VCHowToTopic : UIViewController {
//add handlers so that any manipulation of the title is caught and transferred to the custom drawn UILabel
override var title : String? {
set {
super.title = newValue
configureTitleView()
}
get {
return super.title
}
}
//MARK: - lifecycle
func configureTitleView() {
//some large number that makes the navigationbar schrink down our view when added
let someVeryLargeNumber = CGFloat(4096)
//create our label
let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: someVeryLargeNumber, height: someVeryLargeNumber))
//0 means unlimited number of lines
titleLabel.numberOfLines = 0
//define style of the text (we will be using attributed text)
let style = NSMutableParagraphStyle()
style.alignment = .Center
//top compensate for the backbutton which moves the centered text to the right side of the screen
//we introduce a negative tail indent, the number of 56 has been experimentally defined and might
//depend on the size of your custom back button (if you have one), mine is 22x22 px
style.tailIndent = -56
//create attributed text also with the right color
let attrText = NSAttributedString(string: title!, attributes: [NSParagraphStyleAttributeName : style,
NSForegroundColorAttributeName : UIColor.whiteColor()])
//configure the label to use the attributed text
titleLabel.attributedText = attrText
//add it as the titleview
navigationItem.titleView = titleLabel
}
}
'CGRect(x:0,y:0,width:someVeryLargeNumber,height:someVeryLargeNumber)'是使视图在标题中可见的关键 – 2017-11-24 19:01:35
接受的斯威夫特版本答案+把标签文本的中心:
雨燕2.3:
self.title = "Your TiTle Text"
let tlabel = UILabel(frame: CGRectMake(0, 0, 200, 40))
tlabel.text = self.title
tlabel.textColor = UIColor.whiteColor()
tlabel.font = UIFont.boldSystemFontOfSize(17) //UIFont(name: "Helvetica", size: 17.0)
tlabel.backgroundColor = UIColor.clearColor()
tlabel.adjustsFontSizeToFitWidth = true
tlabel.textAlignment = .Center
self.navigationItem.titleView = tlabel
和SWIFT 3:
self.title = "Your TiTle Text"
let frame = CGRect(x: 0, y: 0, width: 200, height: 40)
let tlabel = UILabel(frame: frame)
tlabel.text = self.title
tlabel.textColor = UIColor.white
tlabel.font = UIFont.boldSystemFont(ofSize: 17) //UIFont(name: "Helvetica", size: 17.0)
tlabel.backgroundColor = UIColor.clear
tlabel.adjustsFontSizeToFitWidth = true
tlabel.textAlignment = .center
self.navigationItem.titleView = tlabel
如果你有一个视图添加到titleView中,并且你想调整视图的大小,你可以使用这个代码(SWIFT 3):
self.translatesAutoresizingMaskIntoConstraints = false
self.layoutIfNeeded()
self.sizeToFit()
self.translatesAutoresizingMaskIntoConstraints = true
检查此链接 http://stackoverflow.com/questions/2422383/uinavigationbar-multi-line-title 它有望解决您的问题。 – gopal 2013-02-19 12:23:36