Swift:How to Find the Position(X,Y) of a Letter in a Uilabel

How to find the position(x,y) of a letter in UILabel

Try this code.

NSRange range = [@"Good,Morning" rangeOfString:@","];
NSString *prefix = [@"Good,Morning" substringToIndex:range.location];
CGSize size = [prefix sizeWithFont:[UIFont systemFontOfSize:18]];
CGPoint p = CGPointMake(size.width, 0);
NSLog(@"p.x: %f",p.x);
NSLog(@"p.y: %f",p.y);

Hope this will help you.

How to get the coordinates (x,y) of a character in a String

Your Swift syntax is really old (Swift 2). Change range.startIndex to range.lowerBound. substringToIndex is now called substring(to: Index) but it is deprecated, you should use subscript self[..<range.lowerBound]. Btw there is no need to use String range(of: String) if you are looking for a index of a character. You can use collection method firstIndex(of: Element):

extension StringProtocol {
func characterPosition(character: Character, with font: UIFont = .systemFont(ofSize: 18.0)) -> CGPoint? {
guard let index = firstIndex(of: character) else {
print("\(character) is missed")
return nil
}
let string = String(self[..<index])
let size = string.size(withAttributes: [.font: font])
return CGPoint(x: size.width, y: 0)
}
}

finding location of specific characters in UILabel on iPhone

The basic tools for measuring text on iPhone are in UIStringDrawing.h but none of them do what you need. You will basically have to iterate through substrings one character at a time measuring each. When a line wraps (the result is taller), split after the last character that did not wrap and add the line height to your y coordinate.

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

In Swift, how to get the y position of a text baseline of a UILabel?

You can achieve this quite simply :

let baselineY = label.frame.origin.y + label.font.ascender;

Swift get CGPoint of last character in UILabel

You should be able to get the width of the text with:

label.intrinsicContentSize.width

Then just add that to the label's X position, assuming the text is left-aligned.

How do I locate the CGRect for a substring of text in a UILabel?

Following Joshua's answer in code, I came up with the following which seems to work well:

- (CGRect)boundingRectForCharacterRange:(NSRange)range
{
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:[self attributedText]];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
[textStorage addLayoutManager:layoutManager];
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:[self bounds].size];
textContainer.lineFragmentPadding = 0;
[layoutManager addTextContainer:textContainer];

NSRange glyphRange;

// Convert the range for glyphs.
[layoutManager characterRangeForGlyphRange:range actualGlyphRange:&glyphRange];

return [layoutManager boundingRectForGlyphRange:glyphRange inTextContainer:textContainer];
}

Determining the position of the end of the last line in UILabel

The best way is to insert your image directly on label by NSTextAttachment and resizing the image as per requirement, in that way you don't have to calculate any spacing and width.


Swift 3 solution

var img_attachment = NSTextAttachment()
img_attachment.image = UIImage(named: "name_of_image")
img_attachment.bounds = CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(ImgWidth), height:CGFloat(ImgHeight)) // you can specify the size and bounds of discount image
var attributedString = NSAttributedString(attachment: img_attachment)
var lblString = NSMutableAttributedString(string: "your text here")
lblString.append(attributedString)
cell.accessoryTitleLabel.attributedText = lblString



Related Topics



Leave a reply



Submit