0

Im trying to add attributed text infront of label at upper right position in swift 3 like [PDF] in picture shown

Please check screen shot

My requirement is... I have a picker view which has text values with list of countries..when a value is selected, I need to show that country name along with its Continent.

I am able show picker view value selected in label which is simple but not sure how to append continent name at upper right position

If I select USA, my label should show NA USA (where NA is North America) with NA in the place of [PDF] and USA in place of "this is sample PDF" from attachment

I can display that by adding uiView in front of label but my requireent clearly says not to add uiview to display this. This should be uilabel with text changing dynamically

Not sure how i can use attributed string to label here

Please guide

Ashh
  • 569
  • 1
  • 6
  • 28
  • Have you made any attempt to use `NSMutableAttributeString` and the label's `attributedText` property? – rmaddy Apr 24 '17 at 02:52
  • I'm not sure how I can display my continents..I could add reference links with NSMutableAttributeString..but how to position it on upper right hand is puzzling me – Ashh Apr 24 '17 at 03:31
  • so as you said not to add UIview, not even two labels? – Nirav Hathi Apr 24 '17 at 05:01
  • This worked like charm. Extension for sub and superscript http://stackoverflow.com/a/37614145/6347717 – Ashh Apr 24 '17 at 11:12

1 Answers1

0

Use this code for that

Output will be 27.67 where .67 will show small and 27 will show large

    let attString:NSMutableAttributedString = NSMutableAttributedString(string: "27.67", attributes: [NSFontAttributeName:Typography.largeDrawerSavingAmountLabel!])
    attString.setAttributes([NSFontAttributeName:Typography.smallDrawerSavingAmountLabel!,NSBaselineOffsetAttributeName:33], range: NSRange(location:attString.string.characters.count - 2,length:2))
    lblTotalAmount.attributedText = attString;

Where

You put your font at place of Typography.largeDrawerSavingAmountLabel and Typography.smallDrawerSavingAmountLabel

Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98
  • Thanks Mike. But I was looking other way around. To align first two characters on top left and rest in bottom..I found this which worked to meet requirement. http://stackoverflow.com/a/37614145/6347717 – Ashh Apr 24 '17 at 11:14