iosswiftswift3uilabeltttattributedlabel

Adding/Appending Attributed text to label in Swift 3 - iOS


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


Solution

  • 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