Superscript cents in an attributed string

MCMatan

I'm trying to get my label to look like so:

enter image description here

But using attributed string, I managed to get this result:

enter image description here

My code:

NSString *string = [NSString stringWithFormat:@"%0.2f",ask];

NSMutableAttributedString *buyString = [[NSMutableAttributedString alloc] initWithString:string];

[buyString addAttribute:NSFontAttributeName
                  value:[UIFont systemFontOfSize:15.0]
                  range:NSMakeRange(2, buyString.length - 2)];

self.labelBuy.attributedText = buyString;

As you see, the numbers after the dot, stay below, and I would like to pop them to the top as the first example. Is there any way to set attributed string frame?

Larme

You have to use NSBaselineOffsetAttributedName.

From the doc:

NSBaselineOffsetAttributeName
The value of this attribute is an NSNumber object containing a floating point value indicating the character’s offset from the baseline, in points. The default value is 0.
Available in iOS 7.0 and later.

From your example:

[buyString addAttribute:NSBaselineOffsetAttributeName
                  value:@(10.0)
                  range:NSMakeRange(2, buyString.length - 2)];

You may have to change the value to fit your needs.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Wrap all cents values in a string with superscript

From Dev

RTF file to attributed string

From Dev

Translate attributed string

From Dev

Email Composer with Attributed String

From Dev

Translate attributed string

From Dev

Superscript string in Swift

From Dev

how to append Attributed Text String with Attributed String in Swift

From Java

How to use Attributed String in SwiftUI

From Dev

Attributed String UILabel text is shrunken

From Dev

Split Attributed String and Retain Formatting

From Dev

ios attributed string is not aligned right

From Dev

Attributed String UILabel text is shrunken

From Dev

Set attributed string to an UILabel in iOS

From Dev

WKInterfaceLabel Attributed String Fails on Width

From Dev

grab just the cents from string

From Dev

grab just the cents from string

From Dev

Find attributes from attributed string that user typed

From Dev

Replace regex match with attributed string and text

From Dev

Error creating attributed string- Swift

From Dev

How to use System Font with an Attributed String?

From Dev

Add a nsstring at the end of attributed string in textview iOS

From Dev

Attributed string hyperlink not showing proper cursor

From Dev

Localising a UILabel with attributed string from a Storyboard

From Dev

Change text of an attributed string and retain attributes in Swift

From Dev

UILabel appearance font and attributed string font

From Dev

Save a UITextView attributed string to file in SwiftUI

From Dev

CoreText Attributed String Height Calculation Inaccurate

From Dev

Replace regex match with attributed string and text

From Dev

How change the font of attributed string in iOS?

Related Related

HotTag

Archive