快速如何让 UILabel 对齐到左上角 [英] swift how to get UILabel align to top left

查看:32
本文介绍了快速如何让 UILabel 对齐到左上角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个琐事应用程序,可以逐字打印问题,目前它从 UILabel 的中间开始,向上推上一个文本.我想知道是否可以从标签左上角的第一个单词开始,然后继续打印其余的单词.

I'm making a trivia app that prints the question out word by word, and currently it starts at the middle of the UILabel and pushes the previous text upward. I'm trying to find out if I can start the first word at the top left of the label, the continue printing the rest.

 func printQuestion ()
    {
        var str = quizbrain.getQuestionText()
        var arr = str.components(separatedBy: " ")
        
        var count = 0

        Timer.scheduledTimer(withTimeInterval: 0.25, repeats: true) { (t) in
            
            self.questionLabel.text! += " " + arr[count] 
            count += 1
            
            if count == arr.count {
                t.invalidate()
            }
            
            if self.hasBuzzed == true {
                t.invalidate()
            }
            
            
        }


推荐答案

我想知道是否可以从标签左上角的第一个单词开始

I'm trying to find out if I can start the first word at the top left of the label

不,你不能.这不是标签在文本更改时绘制的方式.

No, you can’t. That’s not how labels draw when their text changes.

很酷的解决方法:从包含整个文本的标签开始,但使用属性字符串,以便文本与背景颜色相同(例如,白底白字)从而不可见.在您的计时器中,一次将一个单词的颜色更改为黑色.

Cool workaround: start with the label containing the whole text, but use an attributed string so that the text is the same color as the background (e.g. white on white) and thus invisible. In your timer, change the color of one word at a time to black.

这样,您永远不会更改文本,只会更改颜色,因此文本不会随着每个单词的显示而移动.

That way, you never change the text, just the color, so the text does not move as each word is revealed.

这篇关于快速如何让 UILabel 对齐到左上角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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