0

This is what I want to achieve:

enter image description here

I'm thinking about having two separate attributed strings and combine them together. Not sure if this is the only way?

UPDATE

The button displays "(null)" if using setAttributedTitle. It can display the right string with no attributes if using setTitle.

Still cannot display in the intended way. Any idea?

// Set current bar button attributes
NSMutableAttributedString *currentBarAttributedString = [[NSMutableAttributedString alloc] init];
[currentBarAttributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"REQUEST\n"
                                                                         attributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone)}]];
[currentBarAttributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"EQUIPMENT"
                                                                                   attributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)}]];
// Initialize buttons and set titles
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setAttributedTitle:currentBarAttributedString forState:UIControlStateNormal];
// [button1 setTitle:[currentBarAttributedString string] forState:UIControlStateNormal];
Chenya Zhang
  • 463
  • 3
  • 11
  • 22

3 Answers3

1

To add border to text or to change color.here is sample code which is used. use This code in

- (void)viewDidLoad {
  [super viewDidLoad];

  NSString *strFirst = @"Request Equipment";
  NSString *strSecond = @"Active Rentals";


NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];

[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:strFirst
                                                                         attributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),
                                                                            NSForegroundColorAttributeName:[UIColor yellowColor]}]];
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:strSecond
                                                                         attributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone),NSForegroundColorAttributeName:[UIColor whiteColor]}]];
//To use attribute string in button
[self.btnAttributeString setAttributedTitle:attributedString forState:UIControlStateNormal];
}

OutPut is

enter image description here

Please check this and let me know any issue.

Krutarth Patel
  • 3,407
  • 6
  • 27
  • 54
0

Just create a NSAttributedString and format it as required

NSString *alertString = @"All heroes do not wear capes.";

NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentLeft;

NSDictionary *attrs = @{
NSParagraphStyleAttributeName: paragraphStyle,
                        //provide a nsdict here with attributes you want to apply to the whole of the string. 
                        };
NSDictionary *subAttrs = @{
                           NSParagraphStyleAttributeName: paragraphStyle,
                           //Here provide attributes for the not underlined part of the string. 
                           };
NSDictionary *subAttrs2 = @{
                           NSParagraphStyleAttributeName: paragraphStyle,
                           //Here provide attributes for the underlined part of the string
                           };

//Set the range of the sub attributes. 
const NSRange range = NSMakeRange(0,3);
const NSRange range2 = NSMakeRange(5,4);

NSMutableAttributedString *attributedText =
[[NSMutableAttributedString alloc] initWithString:alertString
                                       attributes:attrs];
[attributedText setAttributes:subAttrs range:range];
[attributedText setAttributes:subAttrs2 range:range2];

Now set this attributed string as your attributed title

Shayan Jalil
  • 588
  • 5
  • 17
-1
class UnderlinedLabel: UILabel {

        override var text: String! {

            didSet {
                 let textRange = NSMakeRange(0, count(text))
                let textRange = NSMakeRange(0, text.characters.count)
                let attributedText = NSMutableAttributedString(string: text)
                attributedText.addAttribute(NSUnderlineStyleAttributeName , value:NSUnderlineStyle.StyleSingle.rawValue, range: textRange)
                // Add other attributes if needed

                self.attributedText = attributedText
             }
        }
    }
Ram
  • 961
  • 6
  • 14