How to align uilabel icon with text in iOS

Amira Elsayed Ismail

I have used the following code to add icon to my UILabel

        UIImage *image3 = [UIImage imageNamed:@"icon_comments.png"];

        UIImage *myIcon3 = [self imageWithImage:image3 scaledToSize:CGSizeMake(20, 20)];
        NSTextAttachment *attachment3 = [[NSTextAttachment alloc] init];
        attachment3.image = myIcon3;


        NSAttributedString *attachmentString3 = [NSAttributedString attributedStringWithAttachment:attachment3];
        NSString *temp3 = [NSString stringWithFormat: @"%d", test.noComments];
        NSAttributedString *titleString3 = [[NSAttributedString alloc] initWithString:temp3];

        NSMutableAttributedString *myString3 = [[NSMutableAttributedString alloc] initWithString:@""];
        [myString3 appendAttributedString:attachmentString3];
        [myString3 appendAttributedString:titleString3];


        cell.noComments.attributedText = myString3;

the problem now that the text is not aligned vertically with the icon, can any one tell me what is the problem and how can I solve it

schmidt9

This works in iOS >= 7.0 for some _label

NSRange range = [myString3.string rangeOfString:titleString3.string];
CGFloat dy = (_label.frame.size.height - _label.font.pointSize) / 2;
[myString3 addAttribute:NSBaselineOffsetAttributeName value:[NSNumber numberWithFloat:dy] range:range];

_label.attributedText = myString3;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to change the vertical align of text in a UILabel (iOS8)

From Dev

How to align UILabel text in paragraph

From Dev

QPushButton: How to align icon and text

From Dev

How to align UILabel text from bottom?

From Dev

How to Vertically Align Text Using UILabel and NSAttributedString

From Dev

How to align icon to the left with text to the right?

From Dev

How to align text next to an icon with CSS?

From Java

How to vertically align text with icon font?

From Dev

Align text to the icon

From Dev

css align text with icon with

From Dev

align text and icon CSS

From Dev

css align text with icon with

From Dev

align text and icon CSS

From Dev

UILabel not showing centre align in ios

From Dev

Is this possible to align the text vertically on UILabel?

From Dev

How to vertically align an icon next to text in Bootstrap 3

From Dev

How to make Icon and Text align Left side on JButton

From Dev

How to align icon to the left and text to the center by using flex?

From Dev

Align the facebook icon with the text in the button

From Dev

Align Text Beside FontAwesome Icon

From Dev

How to align a UILabel to a corner of a UIViewController

From Java

How to embed small icon in UILabel

From Java

Vertically align text to top within a UILabel

From Dev

Align UILabel Text to bottom left corner

From Dev

Swift : align UILabel text in the top rather in the middle

From Dev

Vertically align text within a UILabel (xcode)

From Dev

How to change UILabel width based on text length in iOS?

From Dev

How to create multicolor text in UILabel for iOS 5.1 and below

From Dev

iOS - UILabel loading, but no text displaying

Related Related

HotTag

Archive