是否可以删除UITabBarItem中的图像并垂直对齐标题 [英] Is it possible to remove the images in the UITabBarItem and aligned vertically the title

查看:75
本文介绍了是否可以删除UITabBarItem中的图像并垂直对齐标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITabBar,但我不想设置图像,我只想设置标题,并且我希望此标题垂直对齐.这可能吗?

I have a UITabBar but i don't want to set the images I just want to set the title, and I want this title to be aligned vertically. Is this possible?

谢谢

推荐答案

我不知道.我认为您需要创建文本标签的图像(可以预先在所选的图形工具中创建图像,也可以在运行时以编程方式创建它们).

Not that I know of. I think you'd need to create images of your text labels (either in advance in your graphic tool of choice, or you can create them programmatically at runtime).

如果要以编程方式执行此操作,则可以使用如下所示的方法.它使用传递的文字创建居中对齐的图像,然后进行自动换行.

If you want to do it programmatically, a method like the following might do it. It creates a center-aligned image using the text you pass it, performing word-wrap.

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;

这使用一种通过渲染传递给它的文本来创建位图的小方法.您可能需要尝试使用图像大小和字体大小来优化标签栏控制器图像.

This uses a little method that creates a bitmap by rendering the text you pass to it. You might have to play around with image sizes and font sizes to optimize for your tab bar controller images.

- (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;
}

在Swift中,可能看起来像这样:

In Swift, that might look like:

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
}

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;

这篇关于是否可以删除UITabBarItem中的图像并垂直对齐标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆