Tôi có UITabBar
nhưng tôi không muốn đặt hình ảnh tôi chỉ muốn đặt tiêu đề và tôi muốn tựa đề này được căn chỉnh theo chiều dọc. Điều này có thể không?Có thể xóa hình ảnh trong UITabBarItem và căn chỉnh theo chiều dọc tiêu đề
Cảm ơn
Tôi có UITabBar
nhưng tôi không muốn đặt hình ảnh tôi chỉ muốn đặt tiêu đề và tôi muốn tựa đề này được căn chỉnh theo chiều dọc. Điều này có thể không?Có thể xóa hình ảnh trong UITabBarItem và căn chỉnh theo chiều dọc tiêu đề
Cảm ơn
Tôi không biết. Tôi nghĩ bạn cần phải tạo ra hình ảnh của nhãn văn bản của bạn (hoặc trước trong công cụ đồ họa của bạn lựa chọn, hoặc bạn có thể tạo chúng theo chương trình tại thời gian chạy).
Nếu bạn muốn thực hiện theo cách lập trình, một phương pháp như sau có thể thực hiện. Nó tạo ra một hình ảnh liên kết trung tâm bằng cách sử dụng văn bản bạn vượt qua nó, thực hiện từ bọc.
UITabBarItem *item = [self.tabBar.items objectAtIndex:0];
item.image = [self makeThumbnailFromText:@"Tab Bar One"];
item.title = nil;
item = [self.tabBar.items objectAtIndex:1];
item.image = [self makeThumbnailFromText:@"Tab Bar Two"];
item.title = nil;
Điều này sử dụng một phương pháp nhỏ để tạo văn bản mà bạn truyền cho nó. Bạn có thể phải chơi xung quanh với kích thước hình ảnh và kích thước phông chữ để tối ưu hóa cho hình ảnh bộ điều khiển thanh tab của bạn.
- (UIImage *)makeThumbnailFromText:(NSString *)string {
// some variables that control the size of the image we create, what font to use, etc.
CGSize imageSize = CGSizeMake(60, 80);
CGFloat fontSize = 13.0;
NSString *fontName = @"Helvetica-Bold";
UIFont *font = [UIFont fontWithName:fontName size:fontSize];
CGFloat lineSpacing = fontSize * 1.2;
// set up the context and the font
UIGraphicsBeginImageContextWithOptions(imageSize, false, 0);
NSDictionary *attributes = @{NSFontAttributeName: font};
// some variables we use for figuring out the words in the string and how to arrange them on lines of text
NSArray <NSString *> *words = [string componentsSeparatedByString:@" "];
NSMutableArray <NSDictionary *> *lines = [NSMutableArray array];
NSString *lineThusFar;
CGSize sizeThusFar = CGSizeZero;
// let's figure out the lines by examining the size of the rendered text and seeing whether it fits or not and
// figure out where we should break our lines (as well as using that to figure out how to center the text)
for (NSString *word in words) {
NSString *currentLine = lineThusFar ? [NSString stringWithFormat:@"%@ %@", lineThusFar, word] : word;
CGSize size = [currentLine sizeWithAttributes: attributes];
if (size.width > imageSize.width && lineThusFar) {
[lines addObject:@{@"text": lineThusFar, @"size": [NSValue valueWithCGSize: sizeThusFar]}];
lineThusFar = word;
sizeThusFar = [word sizeWithAttributes: attributes];
} else {
lineThusFar = currentLine;
sizeThusFar = size;
}
}
if (lineThusFar) {
[lines addObject:@{@"text": lineThusFar, @"size": [NSValue valueWithCGSize: sizeThusFar]}];
}
// now write the lines of text we figured out above
CGFloat totalSize = (lines.count - 1) * lineSpacing + fontSize;
CGFloat topMargin = (imageSize.height - totalSize)/2.0;
for (NSInteger i = 0; i < lines.count; i++) {
CGFloat x = (imageSize.width - [lines[i][@"size"] CGSizeValue].width)/2.0;
CGFloat y = topMargin + i * lineSpacing;
[lines[i][@"text"] drawAtPoint:CGPointMake(x, y) withAttributes: attributes];
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Trong Swift, có thể trông giống như: chỉ
func makeThumbnailFromText(text: String) -> UIImage {
// some variables that control the size of the image we create, what font to use, etc.
struct LineOfText {
var string: String
var size: CGSize
}
let imageSize = CGSize(width: 60, height: 80)
let fontSize: CGFloat = 13.0
let fontName = "Helvetica-Bold"
let font = UIFont(name: fontName, size: fontSize)!
let lineSpacing = fontSize * 1.2
// set up the context and the font
UIGraphicsBeginImageContextWithOptions(imageSize, false, 0)
let attributes = [NSFontAttributeName: font]
// some variables we use for figuring out the words in the string and how to arrange them on lines of text
let words = text.componentsSeparatedByString(" ")
var lines = [LineOfText]()
var lineThusFar: LineOfText?
// let's figure out the lines by examining the size of the rendered text and seeing whether it fits or not and
// figure out where we should break our lines (as well as using that to figure out how to center the text)
for word in words {
let currentLine = lineThusFar?.string == nil ? word : "\(lineThusFar!.string) \(word)"
let size = currentLine.sizeWithAttributes(attributes)
if size.width > imageSize.width && lineThusFar != nil {
lines.append(lineThusFar!)
lineThusFar = LineOfText(string: word, size: word.sizeWithAttributes(attributes))
} else {
lineThusFar = LineOfText(string: currentLine, size: size)
}
}
if lineThusFar != nil { lines.append(lineThusFar!) }
// now write the lines of text we figured out above
let totalSize = CGFloat(lines.count - 1) * lineSpacing + fontSize
let topMargin = (imageSize.height - totalSize)/2.0
for (index, line) in lines.enumerate() {
let x = (imageSize.width - line.size.width)/2.0
let y = topMargin + CGFloat(index) * lineSpacing
line.string.drawAtPoint(CGPoint(x: x, y: y), withAttributes: attributes)
}
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
Và
var item = tabBar.items![0]
item.image = makeThumbnailFromText("Tab Bar One")
item.title = nil;
item = tabBar.items![1]
item.image = makeThumbnailFromText("Tab Bar Two")
item.title = nil;
Đối nhanh chóng 3
// mục trung tâm của tôi là hình ảnh và các mục thanh tab còn lại chỉ là văn bản
let tabs = CustomTabBarController();
tabs.viewControllers = [ViewControllerOne(),ViewControllerTwo(),ViewControllerThree()]
let tabbar = tabs.tabBar;
tabbar.backgroundColor = UIColor.white
let tabOne = tabbar.items![0]
tabOne.title = "One"
let tabTwo = tabbar.items![1]
tabTwo.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
tabTwo.image = UIImage(named: "tab_icon.png")
let tabThree = tabbar.items![2]
tabThree.title = "Three"
// và trong CustomTabBarController tôi
import UIKit
class CustomTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
UITabBar.appearance().tintColor = UIColor.black
UITabBarItem.appearance().titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -15)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
self.selectedIndex = 1;
}
override var selectedViewController: UIViewController? {
didSet {
guard let viewControllers = viewControllers else {
return
}
for viewController in viewControllers {
if viewController == selectedViewController {
let selected: [String: AnyObject] =
[NSFontAttributeName:fontForTimesRoman(withStyle: "bold", andFontsize: 12),
NSForegroundColorAttributeName: UIColor.black]
viewController.tabBarItem.setTitleTextAttributes(selected, for: .normal)
} else {
let normal: [String: AnyObject] =
[NSFontAttributeName: fontForTimesRoman(withStyle: "regular", andFontsize: 12),
NSForegroundColorAttributeName: UIColor.gray]
viewController.tabBarItem.setTitleTextAttributes(normal, for: .normal)
}
}
}
}
func fontForTimesRoman(withStyle style: String, andFontsize size: CGFloat) -> UIFont {
if (style == "bold") {
return UIFont(name: "TimesNewRomanPS-BoldMT", size: size)!
}
else if(style == "italic"){
return UIFont(name: "TimesNewRomanPS-ItalicMT", size: size)!
}
else{
return UIFont(name: "TimesNewRomanPSMT", size: size)!
}
}
}
ok, cảm ơn, tôi nghĩ rằng thats cách duy nhất quá ... – Pach
@Pach Tôi đã cập nhật câu trả lời của tôi với một số mã bạn về mặt lý thuyết có thể sử dụng để tạo hình ảnh khi chạy. Có lẽ dễ dàng hơn để làm điều đó trong Photoshop hoặc bất cứ công cụ nào bạn muốn. – Rob
Ok cảm ơn rất nhiều! tôi sẽ thử nó! – Pach