【问题标题】:How to know the width of last line of label?如何知道标签最后一行的宽度?
【发布时间】:2015-05-20 13:26:58
【问题描述】:

在我的应用程序中,我有一个标签,其中文本来自服务器,所以我不知道它的宽度,在这个标签的末尾应该直接有一个 UIImage。

我的问题是:由于标签文本的非静态宽度,我不知道如何定位图像。

为了更清楚,这是设计的快照,应该如何:

请问有什么解决这个问题的建议吗?

【问题讨论】:

  • 我建议使用字体符号。假设您的字符串是属性字符串,只需在末尾添加 ► BLACK RIGHT-POINTING POINTER Unicode: U+25BA, UTF-8: E2 96 BA。把它变成蓝色,你就完成了。
  • 我可以给它上色吗?如您所见,它是蓝色的
  • @JurajAntas 我的字符串来自服务器,所以我可以在这里使用你的想法吗?非常感谢:)
  • 在您可用的 textLabel Space 中放置 scrollView 并在 scrollView 中放置您的文本

标签: ios iphone swift uilabel


【解决方案1】:

您可以这样做直接在标签上插入图像

var attachment = NSTextAttachment()
attachment.image = UIImage(named: "your_image_name")

var attributedString = NSAttributedString(attachment: attachment)
var labelString= NSMutableAttributedString(string: "Lorem ipsum dolor sit ame...")
labelString.appendAttributedString(attributedString)

yourUILabel.attributedText = labelString

【讨论】:

  • 非常感谢!太棒了:) 但我可以调整附件的大小吗?因为图像看起来很大
  • @eng_rawan:我想说最好的办法是为您的场景获取正确尺寸的图像
【解决方案2】:

我设法解决了这个问题。这不是最漂亮的代码,但它可以工作。我从标签的最后一行返回单词编号,从中我可以计算出文本结束处和图像开始处的宽度 (x,y) 坐标。

func lastWordInTitle(title: String) -> Int {
    var separateWords: [String] = []
    var sizeOfWords: [CGFloat] = []
    var wordsRemaining: Int = 0
    var wordsWidthInOneLine: CGFloat = 0

    let font = titleLabel.font
    let fontAttr = [NSAttributedStringKey.font: font]

    title.enumerateSubstrings(in: title.startIndex..<title.endIndex, options: .byWords) { (substring, _, _, _) in
        if let substring = substring {
            separateWords.append(substring) // number of words in label
            sizeOfWords.append(substring.size(withAttributes: fontAttr).width + 8) //size of each word + 8 for the space between them
        }
    }
    wordsRemaining = separateWords.count
    print("SSS wordsRemaining initial \(wordsRemaining)")
    var wordsToRemoveIndex = 0

    for index in 0..<separateWords.count {
        wordsWidthInOneLine += sizeOfWords[index]
        wordsToRemoveIndex += 1
        if wordsWidthInOneLine > titleLabel.frame.width {
            if index == separateWords.count - 1  {
                wordsRemaining -= wordsToRemoveIndex
                return 1
            } else {
                wordsRemaining -= wordsToRemoveIndex - 1 == 0 ? 1 : wordsToRemoveIndex - 1
                wordsToRemoveIndex = 0
                wordsWidthInOneLine = 0
                wordsWidthInOneLine = sizeOfWords[index]

            }
        } else if wordsWidthInOneLine < titleLabel.frame.width && index == separateWords.count - 1 {
            let reversedSeparateWordsSize = Array(sizeOfWords.reversed())
            var width: CGFloat = 0
            for index in 0..<wordsRemaining {
                width += reversedSeparateWordsSize[index]
            }
            if width > titleLabel.frame.width {
                return wordsRemaining - 1
            }
            return wordsRemaining
        }
    }

    return wordsRemaining
}

【讨论】:

    【解决方案3】:
    1. 设置UILabel的文本
    2. 使用yourUILabel.frame.width 获取此UILabel 的宽度
    3. 像这样在yourUILabel.frame.width + emptySpace 处设置UIImage 的x 坐标

      var yourUIImageView:UIImageView = UIImageView(frame: CGRectMake(x:PaddingFromLeft + yourUILabel.frame.width + emptySpace, y: yourYCoordinate, width: yourImageWidth, height : yourImageHeight))
      

    【讨论】:

    • 嗨,你的意思是 PaddingFromLeft 和 emptySpace?请问我应该给什么。
    • 如果最后一行的宽度是整个标签宽度的一半呢?如果我没记错的话,问题想把箭头放在标签最后一行的末尾。
    【解决方案4】:

    实际上,这取决于...

    您可能想要使用sizeToFitsizeThatFits 方法。

    【讨论】:

    • sizeToFit 调整标签大小。它的宽度是字符串的宽度。 sizeThatFits 返回相同的大小而不调整标签的大小。在这两种情况下,检查宽度参数,这将是字符串的宽度。
    猜你喜欢
    • 2020-04-30
    • 1970-01-01
    • 2019-03-22
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多