在iPhone上查找UILabel中特定字符的位置

6
我有一个UILabel,其中包含一些文本,比如“Hello World abcdefg”。该标签可以有多行、不同的字体大小等。
问题:我如何找到UILabel中所有字母“d”的坐标?
逻辑上的第一步是在字符串(UILabel.text)中找到这些字符的位置,但是当它实际上在屏幕上绘制时,我如何将其转换为坐标?
想法是找到这些坐标并在该字符上方绘制自定义内容(基本上是用自定义图像覆盖它)。

这个其他答案提供了你可能需要的部分内容。 - progrmr
3个回答

3

在 iPhone 上测量文本的基本工具位于 UIStringDrawing.h 中,但它们中没有一个能满足您的需求。您基本上需要逐个字符迭代子字符串并进行测量。当一行换行时(结果更高),请在未换行的最后一个字符后拆分并将行高添加到您的 y 坐标。

- (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode;

叹气 这需要一些工作量,但我想这是最好的选择。谢谢回复! - Nick

0

iOS 7.0发布以来,方法已经有所改变。请尝试以下方法。

- (CGFloat)charactersOffsetBeforeDayPartOfLabel {
    NSRange range = [[self stringFromDate:self.currentDate] rangeOfString:[NSString stringWithFormat:@"%i",[self dayFromDate:self.currentDate]]];
    NSString *chars = [[self stringFromDate:self.currentDate] substringToIndex:range.location];
    NSMutableArray *arrayOfChars = [[NSMutableArray alloc]init];
    [chars enumerateSubstringsInRange:NSMakeRange(0, [chars length]) options:(NSStringEnumerationByComposedCharacterSequences) usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        [arrayOfChars addObject:substring];
    }];
    CGFloat charsOffsetTotal = 0;
    for (NSString *i in arrayOfChars){
        NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"Helvetica Neue" size:16.0f]};
        charsOffsetTotal += [i sizeWithAttributes:attributes].width;
    }
    return charsOffsetTotal;
}

2
为什么要计算每个单独字符的大小?为什么不一次性获取整个子字符串的大小呢? - rmaddy

0

给你:

fileprivate let selfSizing = UILabel()

class DualColorLabel: UILabel
{
    var filled: UIColor?
    var unfilled: UIColor?
    var origin: String?
    var widths: [CGFloat] = []
    var fuckupLockup = false
    override var text: String? {
        didSet {
            if fuckupLockup {
                print ("SDBOFLAG-13822 wtf?")
            }
        }
    }
    func setupColorsAndText(filled: UIColor,
                    unfilled: UIColor)
    {
        self.filled = filled
        self.unfilled = unfilled
        guard let text = origin, text.count > 0 else {
            assertionFailure("usage error")
            return
        }
        guard font != nil else {
            usageError()
            return
        }
        for index in 1...text.count {
            let s = String.Index(utf16Offset: 0, in: text)
            let e = String.Index(utf16Offset: index, in: text)
            let beginning = text[s..<e]
            let p = String(beginning)
            
            selfSizing.font = font
            selfSizing.text = p
            let size = selfSizing.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude))
            let width = size.width
            widths.append(width)
        }
    }
    
    func setupfill(adjusted: CGRect)
    {
        assert(adjusted.origin.x <= 0, "fixed this code for fill in the middle: currently supported only fill at start")
        let endOffset = adjusted.width + adjusted.origin.x
        guard let font = self.font else {
            usageError()
            return
        }
        guard let origin = origin, let filled = filled,
              let unfilled = unfilled else {
            usageError()
            return
        }
        var idx = String.Index(utf16Offset: origin.count, in: origin)
        for (index, width) in widths.enumerated() {
            if endOffset < width {
                idx = String.Index(utf16Offset: index, in: origin)
                print ("SDBOFLAG-13822 index \(index) for text \(origin)")
                break
            }
        }
        let total = NSMutableAttributedString()
        do {
            let s = String.Index(utf16Offset: 0, in: origin)
            let beginning = origin[s..<idx]
            let p = String(beginning)
            print("SDBOFLAG-13822 filled text \(p)")
            let filledAttributes:
                [NSAttributedString.Key : Any] = [NSAttributedString.Key.foregroundColor:
//                                                    UIColor.yellow,
                                                    filled,
                                                  NSAttributedString.Key.font:
                                                    font
                ]
            
            let filledPortion = NSAttributedString(string: p, attributes: filledAttributes)
            total.append(filledPortion)
        }
        let unfilledAttributes:
            [NSAttributedString.Key : Any] = [NSAttributedString.Key.foregroundColor:
//                                                UIColor.blue,
                                              unfilled,
                                              NSAttributedString.Key.font: font]
        let e = String.Index(utf16Offset: origin.count, in: origin)
        let ending = origin[idx..<e]
        let str = String(ending)
        print("SDBOFLAG-13822 unfilled text \(str)")
        let unfilledPortion = NSAttributedString(string: str, attributes: unfilledAttributes)
        total.append(unfilledPortion)

        self.attributedText = total
        fuckupLockup = true
    }
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}

func usageError()
{
    assertionFailure("usage error")
}

根据提供的建议,片段的宽度计算进入宽度数组。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接