試試這個,我希望這會幫助你,你會得到一個想法如何這個工程─
我有有三段一UISegmentedControl
即_userProfileSagmentOutlet
。下面是示例代碼 -
CGFloat fontSize = 15;
[_userProfileSagmentOutlet setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Roboto-medium" size:fontSize],
NSForegroundColorAttributeName:[UIColor whiteColor]}
forState:UIControlStateSelected];
[_userProfileSagmentOutlet setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Roboto-medium" size:fontSize],
NSForegroundColorAttributeName:[UIColor whiteColor]}
forState:UIControlStateNormal];
這是先前碼截尾標題的像下面圖像 -
這裏是適合每個標題中的段與相同的主邏輯字體大小 -
CGFloat fontSize = 15;
NSAttributedString* firstTitle = [[NSAttributedString alloc] initWithString:@"Membership History" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Roboto-medium" size:fontSize]}];
NSAttributedString* secondTitle = [[NSAttributedString alloc] initWithString:@"Event History" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Roboto-medium" size:fontSize]}];
NSAttributedString* thirdTitle = [[NSAttributedString alloc] initWithString:@"Booked Classes" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Roboto-medium" size:fontSize]}];
float maxW=MAX(MAX(firstTitle.size.width, secondTitle.size.width), thirdTitle.size.width);
while (maxW > _userProfileSagmentOutlet.subviews[0].frame.size.width) {
fontSize--;
firstTitle = [[NSAttributedString alloc] initWithString:@"Membership History" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Roboto-medium" size:fontSize]}];
secondTitle = [[NSAttributedString alloc] initWithString:@"Event History" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Roboto-medium" size:fontSize]}];
thirdTitle = [[NSAttributedString alloc] initWithString:@"Booked Classes" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Roboto-medium" size:fontSize]}];
maxW=MAX(MAX(firstTitle.size.width, secondTitle.size.width), thirdTitle.size.width);
}
[_userProfileSagmentOutlet setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Roboto-medium" size:fontSize],
NSForegroundColorAttributeName:[UIColor whiteColor]}
forState:UIControlStateSelected];
[_userProfileSagmentOutlet setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Roboto-medium" size:fontSize],
NSForegroundColorAttributeName:[UIColor whiteColor]}
forState:UIControlStateNormal];
使用此代碼後圖像看起來像這樣(相同的字體大小和文本適合段和正常工作) -
這是斯威夫特的擴展,如果有人needed-
var fontSize:CGFloat = 15.0;
var firstTitle = NSMutableAttributedString.init(string: "Membership History", attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)])
var secondTitle = NSMutableAttributedString.init(string: "Events History" ,attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)]);
var thirdTitle = NSMutableAttributedString.init(string: "Booked Classes" ,attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)]);
var maxW:CGFloat = max(max(firstTitle.size().width, secondTitle.size().width), thirdTitle.size().width);
while (maxW > userProfileSagmentOutlet.subviews[0].frame.size.width) {
fontSize--;
firstTitle = NSMutableAttributedString.init(string: "Membership History", attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)])
secondTitle = NSMutableAttributedString.init(string: "Events History" ,attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)]);
thirdTitle = NSMutableAttributedString.init(string: "Booked Classes" ,attributes:[NSFontAttributeName: UIFont.systemFontOfSize(fontSize)]);
maxW = max(max(firstTitle.size().width, secondTitle.size().width), thirdTitle.size().width);
}
userProfileSagmentOutlet.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(fontSize),NSForegroundColorAttributeName:UIColor.whiteColor()], forState:UIControlState.Normal)
userProfileSagmentOutlet.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(fontSize),NSForegroundColorAttributeName:UIColor.whiteColor()], forState:UIControlState.Selected)
看到這個http://stackoverflow.com/questions/26453297/scale-uisegmentedcontrol-labels-based-on-width-of-control看這http://stackoverflow.com/questions/22165171/多行的文字顯示一段uisegmentedcontrol被點擊-B你有一些想法 –
@ Anbu.Karthik:謝謝你的鏈接!其實我犯了一點小錯誤,現在解決了! – Lion